Back to cheatsheets
Passwords
Password Storage Best Practices
OWASP-aligned password storage with Argon2id, bcrypt, and PBKDF2 — choose the right algorithm.
What you'll learn
- Argon2id (recommended)
- PBKDF2 (FIPS-140 compliant)
- Adding a pepper
How to use these snippets
- Click the Copy button next to any snippet to copy it to your clipboard.
- Paste the code into your project, IDE, or terminal.
- Replace placeholder values (like
example.comoryour-secret) with your actual data. - Review the code comments for important security notes and caveats.
Code Snippets
Copy-paste code snippets
3 ready-to-use snippets for password storage best practices. Click copy and paste into your project.
Argon2id (recommended)
const argon2 = require('argon2');
// OWASP recommended: 19 MiB, 2 iterations, 1 parallelism
const hash = await argon2.hash(pw, {
type: argon2.argon2id,
memoryCost: 19456, // 19 MiB
timeCost: 2, // iterations
parallelism: 1,
});
// Verify
const valid = await argon2.verify(hash, pw);
PBKDF2 (FIPS-140 compliant)
// FIPS-140 requires PBKDF2 with HMAC-SHA-256
// OWASP: minimum 600,000 iterations
async function pbkdf2Hash(pw, salt) {
const key = await crypto.subtle.importKey(
'raw', new TextEncoder().encode(pw),
'PBKDF2', false, ['deriveBits']
);
const bits = await crypto.subtle.deriveBits({
name: 'PBKDF2',
salt: salt,
iterations: 600000,
hash: 'SHA-256'
}, key, 256);
return new Uint8Array(bits);
}
Adding a pepper
// Pepper = server-side secret stored separately
// from the database. Never in code, use KMS/HSM.
const PEPPER = process.env.PEPPER_SECRET;
async function hashWithPepper(pw) {
const peppered = pw + PEPPER;
return bcrypt.hash(peppered, 12);
}
100% Free · No signup · No cost
Generate something unguessable right now
148 bits of entropy, a crack-time estimate you can trust, and a clipboard that clears itself after 30 seconds.
Part of the Toolly ecosystem · secure.toolly.site