library/GenPhrase/Password.php
public function generate($bits = 50.0)
    {
        $bits = (float) $bits;
        $separators = $this->getSeparators();
        $separatorBits = $this->precisionFloat(log(strlen($separators), 2));
        $passPhrase = '';
        
        try
        {
            if ($bits < self::MIN_ENTROPY_BITS || $bits > self::MAX_ENTROPY_BITS)
            {
                throw new \InvalidArgumentException('Invalid parameter: $bits must be between ' . self::MIN_ENTROPY_BITS . ' and ' . self::MAX_ENTROPY_BITS);
            }
            
            $words = $this->_wordlistHandler->getWordsAsArray();
            $count = count($words);
            if ($count < self::MIN_WORD_COUNT)
            {
                throw new \RuntimeException('Wordlist must have at least ' . self::MIN_WORD_COUNT . ' unique words');
            }

            $countForBits = $count;
            if ($this->_disableWordModifier !== true)
            {
                $countForBits = $countForBits * $this->_wordModifier->getWordCountMultiplier();
            }
            $wordBits = $this->precisionFloat(log($countForBits, 2));
            
            if ($wordBits < 1)
            {
                throw new \RuntimeException('Words does not have enough bits to create a passphrase');
            }
            
            $maxIndex = $count;
            
            if ($this->_disableSeparators === true)
            {
                $useSeparators = false;
            }
            else if ($this->_alwaysUseSeparators)
            {
                $useSeparators = true;
            }
            else
            {
                $useSeparators = $this->makesSenseToUseSeparators($bits, $wordBits, $separatorBits);
            }
            
            do
            {
                $index = $this->_randomProvider->getElement($maxIndex);
                $word = $words[$index];
                
                if ($this->_disableWordModifier !== true)
                {
                    $word = $this->_wordModifier->modify($word, $this->_encoding);
                }

                $passPhrase .= $word;
                $bits -= $wordBits;

                if ($bits > $separatorBits && $useSeparators === true && isset($separators[0]))
                {
					// At least two separator characters
					if (isset($separators[1]))
					{
						$passPhrase .= $separators[$this->_randomProvider->getElement(strlen($separators))];
						$bits -= $separatorBits;
					}
					else
					{
						$passPhrase .= $separators[0];
					}
                }
                else if ($bits > 0.0)
                {
                    $passPhrase .= ' ';
                }
            }
            while ($bits > 0.0);
        }
        catch (\Exception $e)
        {
            throw $e;
        }
        
        return $passPhrase;
    }
public function generate($bits = 50.0)
    {
        $bits = (float) $bits;
        $separators = $this->getSeparators();
        $separatorBits = $this->precisionFloat(log(strlen($separators), 2));
        $passPhrase = '';
        
        try
        {
            if ($bits < self::MIN_ENTROPY_BITS || $bits > self::MAX_ENTROPY_BITS)
            {
                throw new \InvalidArgumentException('Invalid parameter: $bits must be between ' . self::MIN_ENTROPY_BITS . ' and ' . self::MAX_ENTROPY_BITS);
            }
            
            $words = $this->_wordlistHandler->getWordsAsArray();
            $count = count($words);
            if ($count < self::MIN_WORD_COUNT)
            {
                throw new \RuntimeException('Wordlist must have at least ' . self::MIN_WORD_COUNT . ' unique words');
            }

            $countForBits = $count;
            if ($this->_disableWordModifier !== true)
            {
                $countForBits = $countForBits * $this->_wordModifier->getWordCountMultiplier();
            }
            $wordBits = $this->precisionFloat(log($countForBits, 2));
            
            if ($wordBits < 1)
            {
                throw new \RuntimeException('Words does not have enough bits to create a passphrase');
            }
            
            $maxIndex = $count;
            
            if ($this->_disableSeparators === true)
            {
                $useSeparators = false;
            }
            else if ($this->_alwaysUseSeparators)
            {
                $useSeparators = true;
            }
            else
            {
                $useSeparators = $this->makesSenseToUseSeparators($bits, $wordBits, $separatorBits);
            }
            
            do
            {
                $index = $this->_randomProvider->getElement($maxIndex);
                $word = $words[$index];
                
                if ($this->_disableWordModifier !== true)
                {
                    $word = $this->_wordModifier->modify($word, $this->_encoding);
                }

                $passPhrase .= $word;
                $bits -= $wordBits;

                if ($bits > $separatorBits && $useSeparators === true && isset($separators[0]))
                {
					// At least two separator characters
					if (isset($separators[1]))
					{
						$passPhrase .= $separators[$this->_randomProvider->getElement(strlen($separators))];
						$bits -= $separatorBits;
					}
					else
					{
						$passPhrase .= $separators[0];
					}
                }
                else if ($bits > 0.0)
                {
                    $passPhrase .= ' ';
                }
            }
            while ($bits > 0.0);
        }
        catch (\Exception $e)
        {
            throw $e;
        }
        
        return $passPhrase;
    }
public function generate ( $ bits = 50.0 ) { $ bits = ( float ) $ bits ; $ separators = $ this -> getSeparators ( ) ; $ separatorBits = $ this -> precisionFloat ( log ( strlen ( $ separators ) , 2 ) ) ; $ passPhrase = '' ; try { if ( $ bits < self :: MIN_ENTROPY_BITS || $ bits > self :: MAX_ENTROPY_BITS ) { throw new \ InvalidArgumentException ( 'Invalid parameter: $bits must be between ' . self :: MIN_ENTROPY_BITS . ' and ' . self :: MAX_ENTROPY_BITS ) ; } $ words = $ this -> _wordlistHandler -> getWordsAsArray ( ) ; $ count = count ( $ words ) ; if ( $ count < self :: MIN_WORD_COUNT ) { throw new \ RuntimeException ( 'Wordlist must have at least ' . self :: MIN_WORD_COUNT . ' unique words' ) ; } $ countForBits = $ count ; if ( $ this -> _disableWordModifier !== true ) { $ countForBits = $ countForBits * $ this -> _wordModifier -> getWordCountMultiplier ( ) ; } $ wordBits = $ this -> precisionFloat ( log ( $ countForBits , 2 ) ) ; if ( $ wordBits < 1 ) { throw new \ RuntimeException ( 'Words does not have enough bits to create a passphrase' ) ; } $ maxIndex = $ count ; if ( $ this -> _disableSeparators === true ) { $ useSeparators = false ; } else if ( $ this -> _alwaysUseSeparators ) { $ useSeparators = true ; } else { $ useSeparators = $ this -> makesSenseToUseSeparators ( $ bits , $ wordBits , $ separatorBits ) ; } do { $ index = $ this -> _randomProvider -> getElement ( $ maxIndex ) ; $ word = $ words [ $ index ] ; if ( $ this -> _disableWordModifier !== true ) { $ word = $ this -> _wordModifier -> modify ( $ word , $ this -> _encoding ) ; } $ passPhrase .= $ word ; $ bits -= $ wordBits ; if ( $ bits > $ separatorBits && $ useSeparators === true && isset ( $ separators [ 0 ] ) ) { // At least two separator characters if ( isset ( $ separators [ 1 ] ) ) { $ passPhrase .= $ separators [ $ this -> _randomProvider -> getElement ( strlen ( $ separators ) ) ] ; $ bits -= $ separatorBits ; } else { $ passPhrase .= $ separators [ 0 ] ; } } else if ( $ bits > 0.0 ) { $ passPhrase .= ' ' ; } } while ( $ bits > 0.0 ) ; } catch ( \ Exception $ e ) { throw $ e ; } return $ passPhrase ; }
Generates a passphrase based on supplied wordlists, separators, entropy
bits and word modifier.

@param float $bits
@return string
@throws \InvalidArgumentException
@throws \RuntimeException
@throws \RangeException
Generates a passphrase based on supplied wordlists separators entropy bits and word modifier .