Track 07 · Lesson 15 of 18Identity and login
- 01Authentication vs authorization
- 02Browser storage compared
- 03Sessions versus tokens
- 04Inside a JWT
- 05Access and refresh tokens
- 06Where to keep tokens
- 07Choosing a design
- 08The login lifecycle
- 09The token clock
- 10Dead or just unlucky? Refresh decisions
- 11Inside a secure session cookie
- 12Five tabs, one refresh
- 13“Log in with Google”: OAuth and PKCE
- 14Two-factor login
- 15Storing passwords
- 16Logging out properly
- 17Trade-offs and common mistakes
- 18Compare the designs
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
| password column | |
|---|---|
| ana@example.com | sunshine1 |
| ben@example.com | correct horse battery |
| cy@example.com | sunshine1 |
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