Notes
The idea
The idea is simple. At sign-up, the application hashes the password and stores only the hash. At login, it hashes what the user typed and compares the result with the stored value. The password itself is never written to disk or to a log. Because a hash is one-way, someone who reads the table does not get the passwords directly. But as we will see, which hash you choose decides whether that protection is real.
- sign up: hash the password, store only the hash
- log in: hash what was typed, compare with the stored hash
- the password itself is never written anywhere
- a hash is one-way, so the table does not contain the passwords
What not to do
Three approaches fail. Plaintext: the moment the table leaks, every password is known. Encryption: it is reversible, and the key lives on the same server, so the attacker takes both. And a plain fast hash such as MD five, SHA one or even SHA two fifty six. That last one surprises people, because SHA two fifty six is a strong hash. The problem is not strength. It is speed.
- plaintext: everything is lost at once
- encrypted: the key is on the same server; attacker takes both and decrypts
- MD5, SHA-1, SHA-256 alone: far too fast to resist guessing
- all three still appear in real breaches
Why fast is bad
An attacker with the table does not reverse hashes. They guess: hash a candidate password and compare. We computed one million SHA two fifty six hashes in zero point four two seconds, on one core, in plain Python. A graphics card does billions per second. At that speed, every common password, every password from earlier leaks and every short password is found quickly. Hash functions are designed to be fast, and that is exactly wrong here.
# 1,000,000 SHA-256 hashes # one CPU core, plain Python: # 0.42 seconds # a modern GPU: billions of # SHA-256 guesses per second # every 8-character password # and every leaked-password # list falls quickly
Salt
The first defense is a salt: a random value, unique for each user, mixed into the hash and stored beside it. We hashed the same password twice and got two different results. Without salts, identical passwords have identical hashes, and attackers use huge precomputed tables called rainbow tables to look answers up instantly. With salts, those tables are useless, and every user must be attacked separately. A salt is not secret; it only needs to be unique.
bcrypt.hashpw(b"password123", salt1) # $2b$12$... (60 characters) bcrypt.hashpw(b"password123", salt2) # $2b$12$... a different hash # salt: random, unique per user, # stored with the hash, not secret # defeats precomputed tables; # forces one attack per user
Slow on purpose
The second defense is to be slow on purpose. Password hashing functions repeat their work thousands of times. We measured one bcrypt hash at cost twelve: zero point two seven seconds. For a user logging in, a quarter of a second is invisible. For an attacker, it means about four guesses per second per core instead of millions. A list of a billion candidate passwords now takes years for a single user, and every user has a different salt.
SHA-256 0.0000004 s bcrypt, cost 12 0.271 s Argon2id, default 0.132 s # login: a quarter of a second, # nobody notices # attacker: about 4 guesses per # second per core, not millions
Which algorithm
Which algorithm? Argon two i d is the current best choice. It uses a large amount of memory as well as time, and memory is what makes graphics cards and custom chips expensive to use for cracking. A common setting is sixty four megabytes and three passes. Bcrypt is older, well proven, and fine at cost twelve or higher; note that it ignores anything beyond seventy two bytes. Scrypt is also memory-hard. Use PBKDF two only where a standard requires it, with a high round count.
- Argon2id: the current best; uses memory as well as time, which defeats GPUs (64 MB, 3 passes is common)
- bcrypt: older, proven, cost 12 or more; 72-byte limit
- scrypt: also memory-hard
- PBKDF2-SHA256: only when required, 600,000 rounds
Reading a stored hash
A stored hash describes itself. A bcrypt value starts with a version, then the cost, here twelve, meaning two to the power twelve rounds, then the salt and the hash, sixty characters in total. An Argon two value lists the memory, the passes and the threads, then the salt and the hash. Because the settings travel with each hash, you can raise the cost later and old hashes still verify. Recognising these formats is useful in reviews and incident response.
$2b$12$<22 chars salt><31 chars hash> 2b = bcrypt version 12 = cost: 2^12 rounds $argon2id$v=19$m=65536,t=3,p=4$ <salt>$<hash> m = memory in KiB (64 MB) t = passes p = threads # 97 characters in our test
Pepper and upgrades
Two further techniques. A pepper is a secret key kept outside the database, in a key management service, and applied with HMAC before or after hashing. An attacker with only the table cannot even begin to guess. And upgrade on login: when a user signs in and their hash uses old settings, rehash the password with the new ones while you have it. For a legacy table of MD five hashes, wrap each in a strong hash immediately, or force resets.
- pepper: a secret key held outside the database (KMS or HSM), applied with HMAC
- a stolen table alone is then not enough to start guessing
- upgrade on login: if the hash uses old settings, rehash with new ones
- legacy MD5: wrap or reset
Beyond the hash
The hash is one layer. Around it, rate-limit login attempts, because attackers also guess online. Check new passwords against lists of breached passwords, and prefer length over complexity rules. Add multi-factor authentication or passkeys, so a stolen password alone is not enough. Never log passwords, including in debug output and error reports. And return the same message for a wrong user and a wrong password, so the login form does not reveal which accounts exist.
- rate-limit and lock out repeated failures
- check new passwords against breached-password lists
- length over complexity; allow long passphrases
- MFA or passkeys: a stolen password is not enough
- never log passwords; same error for bad user or pass
Common mistakes
Mistakes. Plaintext, or reversible encryption. A fast hash alone, however strong it sounds. No salt, or one global salt for every user. A cost factor chosen ten years ago and never raised as hardware got faster. A home-made scheme instead of a library. Passwords written to logs. A short maximum length, which hints that the password is being stored, not hashed. And no multi-factor authentication on top.
- plaintext or reversible encryption
- MD5, SHA-1 or SHA-256 alone
- no salt, or one salt for all
- a cost factor set years ago
- writing your own scheme
- passwords in logs
- a maximum length of 8 or 16
- no MFA on top
Say it like this
In the interview: never plaintext and never reversible encryption. I use Argon two i d, or bcrypt at cost twelve or more, through a standard library, which gives every user a unique salt. I tune the cost to about a quarter of a second, add a pepper held in a key management service, and rehash on login when settings change. Around that: rate limiting, breached-password checks, multi-factor authentication, and no passwords in logs.
- "Never plaintext or encrypted.
- Argon2id, or bcrypt at cost
- 12+, with a unique salt per user, through a standard
- library. A pepper in a KMS, rehash on login when settings
- change, rate limiting, breach
- checks and MFA, and passwords
- never in logs."