randomPassword.php
Table of Contents
Functions
- randomPassword() : string
- Generates a password nobody chose and nobody has to remember: meant to be copied, not typed from memory.
Functions
randomPassword()
Generates a password nobody chose and nobody has to remember: meant to be copied, not typed from memory.
randomPassword([int $length = 28 ]) : string
The alphabet
Letters and digits only, minus the five that are misread when a password is copied from a
screen or a notebook: l, I, 1, O, 0. That leaves 57 characters. No punctuation, so
the value travels through a shell argument, a TOML or INI string, a URL or a command-line tool
without anything having to escape it.
The length
28 characters out of 57 is about 163 bits. The length is what carries the strength here, and it costs nothing for a password that is copied. A length under 16 is raised to 16, silently: the function answers with a usable password rather than an error, unlike randomHex(), which throws on a length it cannot honour.
Each character is drawn with random_int(), a CSPRNG — never rand() or shuffle(),
which are predictable from a few samples.
Parameters
- $length : int = 28
-
How many characters, 16 at least.
Tags
Return values
string —The password: max( 16 , $length ) characters of [a-km-zA-HJ-NP-Z2-9].