Track 07 · Lesson 15 of 18Identity and login

Track 07 · Identity and login

Storing passwords

Lesson 15 of 18· 7 min

Servers should never store passwords, only a slow, salted hash. Then a leaked database can’t be read, and can’t be cracked quickly either.

Hash three users’ passwords

Real hashing in your browser

The users table, as an attacker sees it after a database leak
emailpassword column
ana@example.comsunshine1
ben@example.comcorrect horse battery
cy@example.comsunshine1
Every password, readable. One leaked backup and every account is gone, plus every other site where people reused the password.

Hashing#

A hash is a one-way function: easy to compute, impossible to reverse. At sign-up the server stores the hash; at login it hashes what was typed and compares. Nobody, including your own team, can read the passwords back.

Salt#

A random value stored next to each hash and mixed into it. Two people with the same password get different hashes, so cracking one doesn’t crack the other, and precomputed tables of common hashes become useless.

Slow on purpose#

SHA-256 and MD5 are built to be fast, which is exactly wrong here: a graphics card tries billions a second. Argon2id, bcrypt and scrypt are built to be slow and memory-hungry, with a cost factor you raise over time as hardware gets faster. PBKDF2 with a high iteration count (the demo uses 600,000) is the option built into browsers and many standard libraries. Use your framework’s password hasher rather than writing your own.

Slow hashes can’t save weak passwords#

“sunshine1” is in every list of common passwords, and no hash makes that list long. Block known-breached passwords at sign-up, allow long passphrases, and add lockout and rate limits at login.

Check yourself

Which is appropriate for storing passwords?