Password Hashing and Salting Explained
On this page
Why password storage matters: assume the database leaks
Breaches happen: a SQL injection, a stolen backup, a misconfigured bucket. When they do, the question is whether attackers get usable passwords. Stored in plain text, every account is immediately compromised, and so are users' accounts on other sites where they reused the password.
Why SHA-256 isn't enough for passwords
Hashing is one-way: you can't reverse SHA-256. But attackers don't reverse it; they guess: hash "password123", compare, repeat. General-purpose hashes are designed to be fast, which helps them. Measured on one laptop CPU core:
SHA-256 (fast, wrong for passwords) 0.0007 ms/hash ~1,499,588 guesses/s per core
bcrypt, cost 12 212.2765 ms/hash ~ 5 guesses/s per core
argon2id, 19 MiB, t=2 (OWASP min) 16.2968 ms/hash ~ 61 guesses/s per coreThat's one CPU core. Attackers use GPUs that compute billions of SHA-256 hashes per second, so most human-chosen passwords hashed with SHA-256 fall in hours. The password hashes are hundreds of thousands of times slower per guess, and Argon2id also needs 19 MiB of memory per guess, which limits how many guesses a GPU can run in parallel.
What a salt does
Without salts, identical passwords produce identical hashes, so one precomputed table (a "rainbow table") cracks every account at once. A salt is a random value generated per password and stored with the hash. Two users with the same password get different hashes, and every guess must be repeated per account.
Modern password hashing functions generate and store the salt for you. From the tests above, using the argon2 Node package:
const hash = await argon2.hash(password, {
type: argon2.argon2id, memoryCost: 19456, timeCost: 2, parallelism: 1,
});
// $argon2id$v=19$m=19456,p=1,t=2$1CyKx74lpCVPV3Q5yE/MQw$fosb1RMSaHCdkktYFA0pbXacyiC2ImxVk+xE8CtNcdU
await argon2.verify(hash, 'correct horse battery staple'); // true
await argon2.verify(hash, 'correct horse battery stapler'); // falseThe stored string contains the algorithm, its parameters, the salt and the hash, so verification needs nothing else, and parameters can be raised later. Hashing the same password twice gave two different strings.
Peppers: a secret on top of the salt
A pepper is one secret value, the same for every password, kept outside the database: in a secrets manager or an HSM (Day 19). You either mix it in before hashing, or encrypt the stored hashes with it. A salt is stored next to the hash, so it only stops precomputation; a pepper means a stolen database alone isn't enough to start guessing. It's optional extra defense, and it makes key rotation your job, so add it when the data is valuable enough to justify that.
Argon2id vs bcrypt vs scrypt vs PBKDF2
Following OWASP's Password Storage Cheat Sheet:
- Argon2id, with at least 19 MiB memory and 2 iterations (or stronger), the first choice.
- scrypt, if Argon2id isn't available.
- bcrypt with cost 10 or more (12 is common), for legacy systems. bcrypt only uses the first 72 bytes of a password.
- PBKDF2 with a high iteration count (600,000 for PBKDF2-HMAC-SHA256), when FIPS compliance requires it.
Tune parameters so a hash takes tens to a few hundred milliseconds on your servers, and re-hash on login when you raise them.
NIST password rules that actually help
NIST's guidance (SP 800-63B) reversed many old habits:
- Require length (at least 8, better 15+ for passwords used alone), allow at least 64 characters and all characters, including spaces.
- No composition rules (forced symbols and digits) and no periodic expiry; they produce predictable passwords like
Summer2026!. - Check new passwords against breached-password lists (such as Have I Been Pwned's k-anonymity API). k-anonymity means you never send the password or even its full hash: you hash it with SHA-1, send only the first 5 characters (
5BAA6forpassword), get back every leaked hash starting with them, several hundred of them, and look for yours locally. The service can't tell which one you were checking. - Rate limit login attempts (Day 24), and offer MFA and passkeys (Day 3).
Check yourself
It's designed to be fast, so attackers with GPUs can test billions of guesses per second against leaked hashes, cracking most human-chosen passwords quickly. Password hashing needs deliberately slow, memory-hard functions like Argon2id or bcrypt.
It makes identical passwords hash to different values, so precomputed rainbow tables are useless, users sharing a password aren't revealed, and attackers must crack each account separately instead of all at once.
Each guess needs that much memory, say 64 MB. GPUs and specialized hardware have lots of compute but limited fast memory per core, so a memory cost sharply limits how many guesses they can run in parallel, while costing your server little for one login.
A salt is random per password and stored with the hash; a pepper is one secret shared by all passwords and kept outside the database, so a database leak alone doesn't expose it.
People respond with predictable variations like Summer2025! to Autumn2025!. Require changes only after evidence of compromise, and check new passwords against breached lists instead.