Why crypto.getRandomValues and not Math.random
Math.random is built to be fast, not unpredictable. In
Chrome and Node it is xorshift128+, and a few dozen of its outputs
are enough to work out its internal state and predict every value
that follows. That is fine for shuffling a playlist and wrong for a
password. This page uses crypto.getRandomValues
instead, which reads from the operating system's cryptographically
secure generator, the same source TLS keys come from.
Getting a random byte is only half the job. Turning it into a
character with value % 62 makes the first few
characters of the alphabet slightly more likely, because 2³² is not
a multiple of 62. The generator throws away the few values that
would cause that bias and draws again, so every character has
exactly the same chance. When you pick more than one kind of
character, a string that misses one of them is redrawn as a whole,
which guarantees each kind appears without making the rest
predictable.
How long should a password be?
Strength is measured in bits: each character adds log₂ of the number of characters it could have been. The table shows bits for common lengths, using this page's alphabets.
| Length | a–z, 0–9 (36) | Letters and numbers (62) | With symbols (90) |
|---|---|---|---|
| 12 | 62 bits | 71 bits | 78 bits |
| 16 | 83 bits | 95 bits | 104 bits |
| 20 | 103 bits | 119 bits | 130 bits |
| 32 | 165 bits | 191 bits | 208 bits |
At a trillion guesses a second, an offline attack on a fast hash, 16 letters and numbers (95 bits) take about 750 million years on average. That is why the default is 16 letters and numbers: strong enough for anything a person types, and accepted by every login form. Symbols are off by default because they add less than you would think, about half a bit per character, and some sites still reject them. Four more characters add more strength than turning symbols on. For API keys, secrets and tokens that nobody types, use 32 or more.
The symbol set leaves out quotes, the backslash and the backtick,
so a generated secret can go into a shell command, a
.env file or JSON without escaping.
Generate a random string in code
JavaScript, in the browser or Node 19 and later, without bias:
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
function randomString(length) {
const limit = 2 ** 32 - (2 ** 32 % chars.length);
const buf = new Uint32Array(1);
let out = "";
while (out.length < length) {
crypto.getRandomValues(buf);
if (buf[0] < limit) out += chars[buf[0] % chars.length];
}
return out;
}
randomString(16); // e.g. "q7RkZ2mWc9TxBv4N"
Python, with the standard secrets module:
import secrets, string
alphabet = string.ascii_letters + string.digits
"".join(secrets.choice(alphabet) for _ in range(16))
A shell, with OpenSSL:
openssl rand -base64 32 | tr -dc 'A-Za-z0-9' | head -c 16
Is it safe to generate a password on a website?
Here, yes: the strings are made by JavaScript on this page and are never sent anywhere. You can check by opening the network tab in your browser's developer tools while you generate, or by turning off your connection after the page loads; it keeps working. Only your settings, such as length and character sets, are remembered, in your browser's local storage. For a password you keep, save it straight into your password manager.
More small utilities live on the tools page, including a short UUID generator and converter.