How to Generate a Random Password in the Browser with the Web Crypto API
Why Math.random is not enough
Math.random() is designed to be fast, not unpredictable. Its output can often be reconstructed if an attacker sees enough values. A password generator needs randomness from a cryptographically secure source. In the browser that source is crypto.getRandomValues(), which fills a typed array with secure random numbers.
Step 1: get an unbiased random integer
A common mistake is to take a random 32-bit number and use % n to pick an index. That slightly favours some characters, because 2^32 is not a multiple of most set sizes. The fix is to throw away values at the top of the range:
function randInt(n) {
const max = Math.floor(0x100000000 / n) * n;
const buf = new Uint32Array(1);
do {
crypto.getRandomValues(buf);
} while (buf[0] >= max);
return buf[0] % n;
}
Step 2: build the generator
We define the character sets, force at least one character from each chosen set, fill the rest from all sets, and shuffle at the end so the forced characters do not sit at the start.
function generatePassword(length = 16, { upper = true, digits = true, symbols = true } = {}) {
const sets = ['abcdefghijklmnopqrstuvwxyz'];
if (upper) sets.push('ABCDEFGHIJKLMNOPQRSTUVWXYZ');
if (digits) sets.push('0123456789');
if (symbols) sets.push('!@#$%^&*()-_=+[]{};:,.<>?');
const all = sets.join('');
const pick = (chars) => chars[randInt(chars.length)];
const out = sets.map(pick);
while (out.length < length) out.push(pick(all));
// Fisher-Yates shuffle using the same secure source
for (let i = out.length - 1; i > 0; i--) {
const j = randInt(i + 1);
[out[i], out[j]] = [out[j], out[i]];
}
return out.slice(0, length).join('');
}
console.log(generatePassword(16));
Step 3: wire it to a page
A number input for the length, three checkboxes and a button are enough. Remember two small things for a good user experience: let people copy the result with navigator. clipboard.writeText(), and never send the generated password anywhere. If it is created and shown on the device, it should stay on the device.
How strong is the result?
Strength comes from the size of the character pool and the length. With all four character types you have roughly 90 possible characters per position. A 16-character password then has about 90^16 combinations, which is far beyond what an attacker can try. Length matters more than complexity: adding four characters helps more than adding one more symbol type.
Common mistakes to avoid
• Using Math.random() or a time-based seed.
• Using % n on a random number without removing the biased range.
• Logging the password to the console in production code.
• Letting the page send the password to an analytics tool or a server by accident.
• Setting a very short maximum length. Allow at least 32 characters, preferably 64.
Want to see it working?
If you want to try a working version before you build your own, the free password generator on GetAllTools lets you choose a length from 6 to 64 characters and works fully in the browser. It also helps when a form asks for an exact size, such as a 15 character password. For storing the result, use a password manager rather than a text file.
Wrap-up
A secure generator needs three things: a secure random source, an unbiased way to pick from it, and a shuffle that does not reuse a weak generator. With about 25 lines of JavaScript you have all three. Try changing the character sets or adding a passphrase mode, and share what you build in the comments.
About the author: John Anderson builds small browser-based tools at GetAllTools.