Password Generator: Why Math.random() Passwords Get Cracked (and What to Use Instead)
Two years ago I shipped a password reset flow where the temporary password came from Math.random().toString(36).slice(2, 14) . Twelve characters, mixed letters and digits. It looked random. Our pen tester asked for two consecutive reset emails, pasted both tokens into a script, and reconstructed the generator's internal state in under a minute. Every "random" password our server could produce was now a short list for him.
That day taught me the difference between unpredictable-looking and unpredictable . I have rewritten that generator three times since, for three different employers, and the bug keeps coming back in code review because Math.random() is one line long and looks completely innocent. Here is how I generate passwords now, in Python, in Node, and when I am too lazy to write code at all.
Why Math.random() fails at this
Math.random() is a pseudorandom number generator, not a secure one.…