Skip to content

Generating passwords

phil/passwords

Both generators draw from Random\Randomizer over the Secure engine — the cryptographically secure source of the system, as random_int() — unless a test gives them a seeded one.

Random passwords

$generator = new PasswordGenerator();
$generator->generate();                                                        // the default policy
$generator->generate(new PasswordPolicy(32, [CharacterSet::Lowercase, CharacterSet::Digits]));
$generator->generate(new PasswordPolicy()->withLength(16)->withoutAmbiguous());
Member of PasswordPolicyDefaultEffect
length204 to 512 characters.
setsthe four CharacterSetsLowercase, uppercase, digits, symbols; repeated sets count once.
excludeAmbiguousfalseLeaves out I, l, 1, |, O, 0 and o.
requireEachSettrueEvery password holds at least one character of each set.

The symbols are !#$%&()*+,-./:;<=>?@[]^_{|}~: no space, quote nor backslash, which other systems often mishandle. Each character is drawn uniformly from the sets together; with each set required, a draw that misses one is drawn again whole, so that every password of the policy stays equally likely.

entropyBits() is the base-2 logarithm of the number of passwords the policy allows, those that miss a required set left out: about 130 bits for the default policy.

Passphrases

$generator = new PassphraseGenerator(Wordlist::french());
$generator->generate();                                                        // seven words joined by dashes
$generator->generate(new PassphrasePolicy(5, ' ', Capitalization::Words, includeDigit: true));
$generator->entropyBits(new PassphrasePolicy());
Member of PassphrasePolicyDefaultEffect
words73 to 64 words, each drawn uniformly and independently.
separator-Up to 8 bytes and no letter, so that it never merges with the words; may be empty.
capitalizationCapitalization::NoneFirst capitalises the first word, Words every word, Random each word at random.
includeDigitfalseAppends a random digit to a random word.

entropyBits() counts the words for an attacker who knows the list and the policy: words × log2(list size), one more bit per word with Random capitalisation, and log2(10 × words) with the digit. Seven words of the bundled lists give 66 to 70 bits.

Wordlists

WordlistWords
Wordlist::english() (en)1 068
Wordlist::french() (fr)866
Wordlist::spanish() (es)723
Wordlist::german() (de)689

The bundled words are common words of three to ten lowercase ASCII letters, without accents, so that a passphrase is typed on any keyboard. Wordlist::of($name, $words) takes the application's own: at least 256 words, each of 1 to 32 characters, without whitespace, none repeated.