Difficulty: Advanced
What is a password pepper, how is it different from a salt, and how would you implement one safely?
A pepper is a secret value mixed into every password hash that is stored outside the database, typically in a secrets manager, KMS or HSM. A salt is unique per user, stored next to the hash and not secret; a pepper is shared across users (or per key version) and must stay secret. If an attacker steals only the database through SQL injection or a leaked backup, they cannot crack any hash without also stealing the pepper.
| Salt | Pepper | |
|---|---|---|
| Secret? | No | Yes |
| Unique per user? | Yes | No, one per key version |
| Stored where | In the database with the hash | Secrets manager, KMS, HSM, env config |
| Protects against | Rainbow tables, spotting reused passwords | Cracking after a database-only leak |
| Required? | Yes, always | Optional defence in depth |
They are complementary. You always salt (your hashing library does it for you) and optionally pepper on top.
HMAC-SHA256(pepper, password), encode the result (base64 or hex), then pass it to Argon2id or bcrypt. Simple, but rotating the pepper requires users to log in again or a nested scheme.With bcrypt, do not feed raw HMAC bytes in: they may contain a null byte, which some implementations treat as end of input, and raw 64-byte digests sit near bcrypt's 72-byte limit. Base64-encode the HMAC first.
pepper_v=2) so you can rotate without breaking existing users.For most apps, a correctly tuned Argon2id or bcrypt with a per-user salt is the essential baseline, and a pepper is extra. For high-value targets such as fintech, UPI-adjacent apps, or large consumer platforms where a leaked database would be headline news, adding a KMS-backed pepper is a reasonable defence-in-depth measure. Never use a pepper as an excuse for a fast hash.
import crypto from 'node:crypto';
import argon2 from 'argon2';
// Loaded at startup from a secrets manager, never from the DB or Git
const PEPPERS = {
2: Buffer.from(process.env.PEPPER_V2, 'base64'),
1: Buffer.from(process.env.PEPPER_V1, 'base64'), // kept for old hashes
};
const CURRENT = 2;
const pre = (pw, v) =>
crypto.createHmac('sha256', PEPPERS[v]).update(pw).digest('base64');
export async function hash(pw) {
return { version: CURRENT, hash: await argon2.hash(pre(pw, CURRENT), { type: argon2.argon2id }) };
}
export async function verify(pw, row) {
const ok = await argon2.verify(row.hash, pre(pw, row.version));
if (ok && row.version !== CURRENT) {
// Re-hash with the new pepper after a successful login
return { ok, upgrade: await hash(pw) };
}
return { ok };
}
The base64 HMAC output is safe to feed into any password hash. Storing the pepper version lets you rotate peppers gradually as users log in.
Password Pepper, Salt, HMAC, Key Management, Defence in Depth