Pepper vs. Salt

Difficulty: Advanced

Question

What is a password pepper, how is it different from a salt, and how would you implement one safely?

Answer

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 vs pepper

SaltPepper
Secret?NoYes
Unique per user?YesNo, one per key version
Stored whereIn the database with the hashSecrets manager, KMS, HSM, env config
Protects againstRainbow tables, spotting reused passwordsCracking after a database-only leak
Required?Yes, alwaysOptional defence in depth

They are complementary. You always salt (your hashing library does it for you) and optionally pepper on top.

Two ways to apply a pepper

  1. Pre-hash with HMAC: compute 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.
  2. Encrypt the final hash: hash with Argon2id or bcrypt as normal, then encrypt the hash with AES-GCM using a key from KMS. OWASP's Password Storage Cheat Sheet describes both; encryption makes key rotation easier because you can decrypt and re-encrypt stored hashes without the user's password.

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.

Operational realities

  • Storage: never in the same database, never in source code committed to Git. Use AWS Secrets Manager, GCP Secret Manager, HashiCorp Vault or an HSM. Ideally the HMAC or encryption happens inside the KMS/HSM so the key never sits in app memory.
  • Versioning: store a pepper or key ID with each hash (pepper_v=2) so you can rotate without breaking existing users.
  • Loss is catastrophic: if you lose the pepper, no one can log in. Back it up as carefully as you protect it.
  • Limited protection: if the attacker fully compromises an application server, they can likely read the pepper too. It helps most against SQL injection, leaked backups and stolen database snapshots, which are common breach paths.

When is it worth it?

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.

Peppered password hashing. The pepper lives outside the database, so a stolen table alone is not enough to crack.
  1. User password: Typed at signup or login.
  2. HMAC with pepper: Secret key fetched from KMS or Vault.
  3. Argon2id with per-user salt: Slow hash; salt embedded in output.
  4. Store hash and pepper version: Database never holds the pepper.

Code examples

HMAC pepper before Argon2id with key versioning

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.

Key points

  • Salt: per user, public, stored with the hash. Pepper: shared, secret, stored elsewhere
  • A pepper protects against database-only leaks such as SQL injection or stolen backups
  • Apply via HMAC before hashing or by encrypting the final hash with a KMS key
  • Version your pepper so it can be rotated; losing it locks everyone out
  • A pepper never replaces a slow, salted algorithm like Argon2id or bcrypt

Common follow-up questions

  • How would you rotate a pepper without forcing every user to reset their password?
  • Why is encrypting the hash easier to rotate than an HMAC pepper?
  • Where exactly would you store the pepper in an AWS deployment?
  • Does a pepper help if the application server itself is compromised?

Interview tips

  • Frame the pepper around a specific threat: database stolen but app servers intact
  • Be honest about limits; saying it does not help against full server compromise shows maturity
  • Mention rotation and versioning; most candidates forget the operational side

Concepts covered

Password Pepper, Salt, HMAC, Key Management, Defence in Depth

More Web Security (OWASP, JWT, XSS, CSRF) questions