Password Hashing Best Practices for Developers
Why Password Hashing Matters
Never store passwords in plaintext! If your database is breached:
- Plaintext: Attackers have all passwords immediately
- Hashed: Attackers must crack each hash individually
The Problem with Simple Hashing
Using SHA-256 directly on passwords is not secure:
`` password123 → SHA-256 → ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f
`
Problems:
The Solution: Salting
A salt is random data added to each password before hashing:
` password123 + randomsalt123 → hash password123 + differentSalt → completely different hash
`
Benefits:
- Defeats rainbow tables
- Same password produces different hashes for different users
- Attacker must crack each hash individually
Modern Password Hashing Algorithms
bcrypt
` $2b$12$LQv3c1yqBwEHxPzVu3Nfx.6YH2cR/uvgNmNpNkw7J8fQr.l2Cm/Oy The work factor (cost) should make hashing take ~100-500ms: bcrypt:
`
`Argon2
scrypt
Implementation Guidelines
DO:
DON'T:
Work Factor Recommendations
javascript
// Cost factor of 12 is a good starting point
const hash = await bcrypt.hash(password, 12);
`
Argon2:
`javascript
// Memory: 64MB, Time: 3 iterations, Parallelism: 4
const hash = await argon2.hash(password, {
memoryCost: 65536,
timeCost: 3,
parallelism: 4
});
``
Password Verification Flow
Additional Security Measures
Summary
For secure password storage in 2025: