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

A password generator with unbiased crypto randomness and real entropy numbers

Every password generator makes two quiet decisions for you: where the randomness comes from, and whether the password leaves your machine. I built one where the answers are "your browser's crypto source" and "nowhere": F

Every password generator makes two quiet decisions for you: where the randomness comes from, and whether the password leaves your machine. I built one where the answers are "your browser's crypto source" and "nowhere": Free Password Generator

Unbiased picks from crypto.getRandomValues

Math.random() is not good enough here: it is not a cryptographic source, and its output can be predicted from earlier outputs. crypto.getRandomValues is the same source the browser uses for keys.

But even with good random bytes, byte % pool.length quietly skews the result toward the lower characters whenever the pool size does not divide 256 evenly. The fix is rejection sampling: throw away values in the skewed tail and draw again.

function rand(max) { // unbiased integer in [0, max)
  const range = Math.floor(0x100000000 / max) * max;
  const buf = new Uint32Array(1);
  let x;
  do { crypto.getRandomValues(buf); x = buf[0]; } while (x >= range);
  return x % max;
}

Show the entropy, not a colored bar

"Strength meters" that turn green are vibes. Entropy is a number you can check: length times log2 of the pool size. 16 characters over an 89-character pool is about 104 bits. The tool prints that line under every password, so each checkbox shows exactly what it buys you.

Local or nothing

A password typed or made on someone else's server asks you to trust their logs. This one is a single HTML file with no network calls after the page loads. Disconnect and it still works; view source and you can read the whole thing.

Try it - free, no signup. Curious how you handle the copy-to-clipboard step in your own tools.

📰 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.