Storing passwords: hashing, salts and slow on purpose
Assume your database will leak one day. What decides whether that's an embarrassment or a disaster is how you stored the passwords. Why fast hashes are the wrong tool and what to use instead.
In one sentence
Never store passwords, store slow, salted hashes made with Argon2id or bcrypt. MD5 and SHA-256 are built to be fast, and fast is exactly what an attacker with GPUs wants.
The idea
A hash is a one-way function: easy to compute, impossible to reverse. But attackers don't reverse hashes; they guess passwords, hash each guess and compare. The only thing that matters is how many guesses per second they can make.
⚠ In every breach wordlist: cracked instantly, whatever the hash.
What good storage looks like
| Method | Verdict | Why |
|---|---|---|
| Plain text / encryption | Never | One leaked key or query reveals every password |
| MD5, SHA-1, SHA-256 | No | Billions of guesses per second on one GPU |
| SHA-256 + salt | Still no | Salt stops precomputed tables but not fast guessing |
| bcrypt (cost ≥ 12) | Good | Deliberately slow; widely supported |
| Argon2id | Best default | Slow and memory-hard, so GPUs lose most of their advantage |
import { hash, verify } from "@node-rs/argon2";
const stored = await hash(password); // salt + params included
const ok = await verify(stored, attempt); // constant-time compare
// stored looks like: $argon2id$v=19$m=19456,t=2,p=1$<salt>$<hash>A salt is a random value stored next to each hash, so two users with the same password get different hashes and attackers can't crack everyone at once. Modern libraries add it for you.
Beyond the hash
- Check new passwords against breach lists (e.g. the k-anonymity Pwned Passwords API) and reject the known ones.
- Length beats complexity rules: allow long passphrases and paste from password managers; drop forced symbols and periodic resets.
- Rehash on login when you raise the cost or switch algorithms.
- A pepper (a secret kept outside the database) adds a layer if only the DB leaks.
Key takeaways
- 01Store salted, slow hashes: Argon2id or bcrypt, never MD5/SHA.
- 02Attackers guess; slow hashes make every guess expensive.
- 03Reject breached passwords and allow long passphrases.
- 04Passkeys avoid storing a crackable secret at all.
Check your understanding
1 of 3
How should passwords be stored?
Related concepts
Brute force & rate limits
Limit guesses per IP and per account, slow down rather than lock out, block known breached passwords, and require MFA so a correct guess still isn't enough.
6 min read →MFA & passkeys
Any MFA beats a password alone. But only phishing-resistant methods, passkeys and security keys, stop a real-time phishing proxy, because the browser checks the domain instead of trusting the human.
7 min read →Defense in depth
Every control has holes. Line up several independent ones and the holes rarely align: the Swiss cheese model.
6 min read →