Audit password hashing
Passwords must be stored with a slow, salted, memory-hard function; anything fast enough to brute
force at scale (MD5, SHA-1, SHA-256, even with a salt) is inadequate. The parameters matter as much
as the algorithm name.
Procedure
Find the hashing call and its algorithm:
rg -n "md5|sha1|sha256|sha512|hashlib|bcrypt|argon2|scrypt|pbkdf2|password_hash|bcrypt\." src/ \
| rg -i "pass|credential|secret"
Reject fast hashes outright. A salt stops rainbow tables but not per-password GPU brute force;
SHA-256 at ~billions/sec falls in minutes.
Prefer, in order: Argon2id, scrypt, bcrypt. Check the cost parameter, not just the function:
| Algo |
Minimum sane setting (2024) |
| bcrypt |
cost 12 (≈250 ms) |
| Argon2id |
m=19456 (19 MiB), t=2, p=1 |
| scrypt |
N=2^15, r=8, p=1 |
| PBKDF2 |
600 000+ SHA-256 iterations (last resort) |
Confirm salts are per-password and random (they are embedded in bcrypt/argon2 strings). A single
global salt or no salt is a finding.
Verify the comparison is constant-time. == on the digest leaks via timing; libraries expose a
safe check:
import bcrypt
if bcrypt.checkpw(password.encode(), stored_hash): ... # constant-time, parse-safe
# Python hmac: hmac.compare_digest(a, b)
Handle algorithm migration: store the algorithm in the hash string and re-hash on next successful
login when the cost is below target.
if bcrypt.checkpw(pw, h) and bcrypt.cost(h) < 12:
store(bcrypt.hashpw(pw, bcrypt.gensalt(12)))
Cap input length and normalise Unicode (NFC) before hashing; bcrypt truncates at 72 bytes, so
pre-hash with SHA-256 if longer passwords must be supported.
Pitfalls
hashlib.sha256(salt + password) with a per-user salt is still a fast hash and is not acceptable.
- bcrypt cost 4-8 (test defaults) ships to production all the time; verify the real parameter.
- Peppers (an app-wide secret mixed in) help but do not replace a slow algorithm, and a rotated
pepper invalidates every hash.
- Comparing hashes with
== in Python/JS is a timing oracle.
- Silent truncation: two long passwords sharing the first 72 bytes collide under plain bcrypt.
- Re-hashing only on password change leaves the whole user base on the old parameter for years.
Verification
python3 -c "import bcrypt,sys; h=sys.argv[1].encode(); print('cost', bcrypt.cost(h))" '$HASH'
rg -n "hashlib\.(md5|sha1|sha256)" src/ && echo "FAST HASH PRESENT" || echo "no fast hash"
Pass: every stored hash uses Argon2id/scrypt/bcrypt at or above the table's setting, no fast hash
appears in password code, and comparison is via the library's check function. Report the algorithm,
cost, salt source, and the migration path.
1---2name: audit-password-hashing3description: Use when reviewing how user passwords are stored or verified. Checks for a memory-hard algorithm and cost factor, rules out fast hashes, and verifies comparison is constant-time.4metadata:5 source: https://skillmd.com/skills/jiayaoqijia/audit-password-hashing6---78# Audit password hashing910Passwords must be stored with a slow, salted, memory-hard function; anything fast enough to brute11force at scale (MD5, SHA-1, SHA-256, even with a salt) is inadequate. The parameters matter as much12as the algorithm name.1314## Procedure15161. Find the hashing call and its algorithm:1718 rg -n "md5|sha1|sha256|sha512|hashlib|bcrypt|argon2|scrypt|pbkdf2|password_hash|bcrypt\." src/ \19 | rg -i "pass|credential|secret"20212. Reject fast hashes outright. A salt stops rainbow tables but not per-password GPU brute force;22 SHA-256 at ~billions/sec falls in minutes.23243. Prefer, in order: Argon2id, scrypt, bcrypt. Check the cost parameter, not just the function:2526 | Algo | Minimum sane setting (2024) |27 |---|---|28 | bcrypt | cost 12 (≈250 ms) |29 | Argon2id | m=19456 (19 MiB), t=2, p=1 |30 | scrypt | N=2^15, r=8, p=1 |31 | PBKDF2 | 600 000+ SHA-256 iterations (last resort) |32334. Confirm salts are per-password and random (they are embedded in bcrypt/argon2 strings). A single34 global salt or no salt is a finding.35365. Verify the comparison is constant-time. `==` on the digest leaks via timing; libraries expose a37 safe check:3839 import bcrypt40 if bcrypt.checkpw(password.encode(), stored_hash): ... # constant-time, parse-safe41 # Python hmac: hmac.compare_digest(a, b)42436. Handle algorithm migration: store the algorithm in the hash string and re-hash on next successful44 login when the cost is below target.4546 if bcrypt.checkpw(pw, h) and bcrypt.cost(h) < 12:47 store(bcrypt.hashpw(pw, bcrypt.gensalt(12)))48497. Cap input length and normalise Unicode (NFC) before hashing; bcrypt truncates at 72 bytes, so50 pre-hash with SHA-256 if longer passwords must be supported.5152## Pitfalls5354- `hashlib.sha256(salt + password)` with a per-user salt is still a fast hash and is not acceptable.55- bcrypt cost 4-8 (test defaults) ships to production all the time; verify the real parameter.56- Peppers (an app-wide secret mixed in) help but do not replace a slow algorithm, and a rotated57 pepper invalidates every hash.58- Comparing hashes with `==` in Python/JS is a timing oracle.59- Silent truncation: two long passwords sharing the first 72 bytes collide under plain bcrypt.60- Re-hashing only on password change leaves the whole user base on the old parameter for years.6162## Verification6364 python3 -c "import bcrypt,sys; h=sys.argv[1].encode(); print('cost', bcrypt.cost(h))" '$HASH'65 rg -n "hashlib\.(md5|sha1|sha256)" src/ && echo "FAST HASH PRESENT" || echo "no fast hash"6667Pass: every stored hash uses Argon2id/scrypt/bcrypt at or above the table's setting, no fast hash68appears in password code, and comparison is via the library's check function. Report the algorithm,69cost, salt source, and the migration path.