05 / 11 · Modern cryptography
Password hashing
Why sites store a salted, deliberately slow hash instead of your password.
- Recommended
- Argon2id (RFC 9106)
- Also fine
- scrypt, bcrypt, PBKDF2
- Key ideas
- Unique salt, slow on purpose
Websites need to check your password without keeping it. If they store it as typed, a single database leak exposes every account, and since people reuse passwords, accounts on other sites too.
The answer is a password hash: a one-way function that is salted, so identical passwords look different, and slow, so every guess costs an attacker real time. Below, your browser runs the real computations: SHA-256, salts and PBKDF2.
user
password
01 / 12 · Password storage
Never store the password
A site that saves passwords exactly as typed hands all of them to whoever steals the database. Breaches happen; what matters is what they leak.
People reuse passwords, so one leaked list also opens their accounts on other sites.
How it works
- 1When you sign up, the server draws a random salt (16 bytes is typical) and stores it next to the result.
- 2It runs a password hashing function such as Argon2id, bcrypt, scrypt or PBKDF2 on the password and the salt, with a work factor that makes one computation take a noticeable fraction of a second.
- 3The stored string records the algorithm, its parameters, the salt and the hash, for example $argon2id$v=19$m=19456,t=2,p=1$… so the parameters can be raised later.
- 4At login, the server repeats the computation with the stored salt and parameters and compares the results. The password itself is never stored.
Why it holds
- Salts defeat precomputation
- A precomputed table maps the hashes of common passwords back to the passwords. With a random salt per user, such a table would have to be rebuilt for every salt, and two users with the same password no longer share a hash.
- Work factors defeat speed
- A single GPU computes on the order of 10¹⁰ SHA-256 hashes per second. PBKDF2 with 600,000 iterations costs about 1.2 million SHA-256 computations per guess, cutting the same GPU to roughly 8,000 guesses per second.
- Memory hardness
- Argon2id and scrypt make each guess fill and reread a large block of memory. Attackers' GPUs and ASICs have many cores but little memory per core, so memory, not compute, caps how many guesses run in parallel.
What can go wrong
- Weak passwords still fall
- A slow hash multiplies the cost of each guess; it cannot help a password that is among the first million an attacker tries. Length and randomness matter: six random diceware words give about 77 bits, eight random lowercase letters only about 38.
- Online guessing and reuse
- Hashing protects a stolen database, not the login form. Rate limiting, breached-password checks and multi-factor authentication cover what hashing cannot, including passwords already leaked elsewhere.