Dev.to Security 🔐 Cybersecurity 👁 0 📖 3 min read

My password generator used crypto.getRandomValues and was still wrong 15% of the time

My site has a small client-side password generator. It has a length slider, four checkboxes (A–Z, a–z, 0–9, symbols) and crypto.getRandomValues. I assumed it was done. Then I went back to it while writing a blog post, an

My site has a small client-side password generator. It has a length slider, four checkboxes (A–Z, a–z, 0–9, symbols) and crypto.getRandomValues. I assumed it was done. Then I went back to it while writing a blog post, and it had two problems. One was mostly theoretical. The other was very practical.

The original code

let s = '';
if (up.checked) s += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (lo.checked) s += 'abcdefghijklmnopqrstuvwxyz';
if (nu.checked) s += '0123456789';
if (sy.checked) s += '!@#$%^&*()-_=+[]{};:,.?';

const a = new Uint32Array(L);
crypto.getRandomValues(a);
let p = '';
for (let i = 0; i < L; i++) p += s[a[i] % s.length];

It uses a cryptographic RNG, so it looks fine. Here's what's wrong with it.

Problem 1: "Password must contain a number"

Ticking "0–9" only means digits are allowed. Nothing guarantees one shows up. With all four groups ticked, the combined set has 85 characters, and only 10 of them are digits.

I simulated 200,000 passwords at each length and counted how many were missing at least one of the ticked groups:

Length Missing a ticked group
6 70.6%
8 51.0%
12 26.5%
16 (my default) 14.6%

So roughly one in seven passwords at the default length would be rejected by a sign-up form that requires "at least one number and one symbol". The user ticked the boxes, copied the password, and the website said no. That's a real bug, not a theory.

Problem 2: modulo bias

a[i] % s.length maps 2³² possible values onto 85 characters. 2³² isn't a multiple of 85 (the remainder is 1), so one character gets picked very slightly more often than the others. Here the difference is about 2 in 10 billion, which nobody will ever exploit. But the fix is three lines, and it matters more when the set size is large relative to the random range. So:

// unbiased integer in [0, n)
function rnd(n) {
  const lim = Math.floor(2 ** 32 / n) * n;   // largest multiple of n
  const b = new Uint32Array(1);
  do { crypto.getRandomValues(b); } while (b[0] >= lim);
  return b[0] % n;
}

This is rejection sampling: throw away the few values from the incomplete top range and draw again. For small n a retry almost never happens.

The fix for problem 1

Pick one character from each ticked group first, fill the rest from the combined set, then shuffle so the guaranteed characters aren't always at the start:

const sets = [];   // only the ticked groups
// ...push each ticked group's characters...
const all = sets.join('');

const c = sets.map(g => g[rnd(g.length)]);   // one from each group
while (c.length < L) c.push(all[rnd(all.length)]);

for (let i = c.length - 1; i > 0; i--) {     // Fisher–Yates
  const j = rnd(i + 1);
  [c[i], c[j]] = [c[j], c[i]];
}
const password = c.join('');

I ran 20,000 passwords of length 6 with all groups ticked through the new version. All 20,000 contained every group.

Does guaranteeing one of each make the passwords weaker? Slightly, in theory, because the space is a little smaller than "any 16 of 85". In practice it's negligible. A 16-character password with all four groups is still around 100 bits of entropy, and the alternative is a password the website refuses.

The lesson

Using crypto.getRandomValues makes a generator secure. It doesn't make it correct. The bug that hurt users was in the logic around the RNG: what the checkboxes promise versus what the code guarantees. If you've written a generator like this, run a quick simulation over its output. It takes five minutes.

The generator is part of 14FreeTools, a set of free tools that run entirely in the browser. Nothing you generate is sent anywhere.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.