Password Hashing Done Right: Argon2id, bcrypt, scrypt and Peppers
How to store passwords safely: why fast hashes fail, Argon2id vs bcrypt vs scrypt, choosing parameters, salts and peppers, rehashing.
Published December 6, 2026
If your user database leaks, password storage decides whether attackers get a list of hashes they cannot crack or millions of reusable passwords. General-purpose hashes like MD5 and SHA-256 are designed to be fast, which is exactly wrong for passwords: GPUs can try billions of guesses per second. Password hashing functions are deliberately slow and memory-hungry.
The concept
Modern recommendations (OWASP Password Storage Cheat Sheet) favour Argon2id, the winner of the Password Hashing Competition, with memory-hard parameters that make GPU and ASIC cracking expensive. scrypt is also memory-hard. bcrypt remains acceptable and widely supported, though it is not memory-hard and truncates inputs at 72 bytes. PBKDF2 is used where FIPS compliance is required, with very high iteration counts.
Each password gets a unique random salt, which these algorithms generate and embed in the encoded hash string automatically, defeating precomputed tables. An optional pepper, a secret key stored outside the database (in a secrets manager or HSM), adds protection if only the database leaks. Store algorithm parameters with the hash so you can raise costs over time and rehash on next login.
Why fast hashes fail
Choosing an algorithm
| Algorithm | Memory-hard | Notes | Recommendation |
|---|---|---|---|
| Argon2id | Yes | Tunable memory, time, parallelism | First choice |
| scrypt | Yes | Good alternative | Acceptable |
| bcrypt | No | 72-byte input limit; widely available | Acceptable (cost ≥ 10-12) |
| PBKDF2-HMAC-SHA256 | No | FIPS-friendly; needs very high iterations | When compliance requires |
| MD5 / SHA-x (plain) | No | Too fast | Never for passwords |
Lifecycle
- On login, verify against stored hash
- If parameters are outdated, rehash with current parameters
- Store the new encoded hash
- Check new passwords against breached-password lists
- Rate limit and monitor login attempts
Practical examples
Argon2id in Python
argon2-cffi handles salts and encoding.
from argon2 import PasswordHasher
ph = PasswordHasher(time_cost=3, memory_cost=64 * 1024, parallelism=1) # 64 MiB
stored = ph.hash("correct horse battery staple")
ph.verify(stored, "correct horse battery staple") # raises on mismatch
if ph.check_needs_rehash(stored):
stored = ph.hash(password)PHP built-ins
password_hash with Argon2id (if compiled in) or bcrypt by default.
$hash = password_hash($password, PASSWORD_ARGON2ID);
if (password_verify($password, $hash) && password_needs_rehash($hash, PASSWORD_ARGON2ID)) {
$hash = password_hash($password, PASSWORD_ARGON2ID); // upgrade on login
}Breached password check with k-anonymity
Only a 5-character hash prefix leaves your server.
import hashlib, requests
sha1 = hashlib.sha1(password.encode()).hexdigest().upper()
prefix, suffix = sha1[:5], sha1[5:]
resp = requests.get(f"https://api.pwnedpasswords.com/range/{prefix}", timeout=5)
breached = any(line.split(":")[0] == suffix for line in resp.text.splitlines())Common mistakes
- Using SHA-256 or MD5 (even salted) for passwords.
- Rolling your own salting and comparison code.
- Cost parameters chosen once and never increased.
- Storing the pepper in the same database as the hashes.
Key takeaways
- Use slow, memory-hard password hashes: Argon2id first, bcrypt/scrypt acceptable.
- Unique salts are built in; consider a pepper stored separately.
- Rehash on login when parameters change.
- Block breached passwords and rate limit logins.
FAQ
How long should hashing take?
Aim for roughly tens to a few hundred milliseconds per hash on your servers, balancing security and login throughput.
Should I encrypt passwords instead?
No. Encryption is reversible; passwords should be hashed so even you cannot recover them.
Do passkeys make this unnecessary?
For passkey-only accounts, yes, no password is stored. During the transition, keep passwords hashed properly.