Articles

Password Salt vs Pepper: What Each One Protects Against

A salt is a unique, public value per password; a pepper is one secret kept outside the database. How they differ, how to combine them, and common mistakes.

Chisato Chisato · · 5 min read
A red padlock icon over a keyboard

A salt is a random value generated uniquely for each password and stored alongside its hash; a pepper is a single secret value applied to every password and stored outside the database. Salts make each hash unique so attackers cannot crack many passwords at once. Peppers add a secret the attacker does not have, so a stolen database on its own is not enough to start cracking. They defend against different things, which is why the strongest password storage uses both.

The baseline: slow, salted hashing

Before getting to the differences, it helps to be clear about the foundation. Passwords should never be stored in plain text or encrypted with a reversible key. They should be run through a deliberately slow, memory-hard password hashing function such as Argon2, scrypt, or bcrypt. Our guide to bcrypt and Argon2 covers how to choose and tune one. The point of a slow function is to make every guess expensive for an attacker. Fast general-purpose hashes are designed for the opposite — see hashing vs encryption for why that distinction matters.

Salt and pepper are additions on top of that slow hash, not substitutes for it.

What a salt does

A salt is a random string, typically 16 bytes or more from a cryptographically secure generator, created fresh for every password at the moment it is set. The hash is computed over the salt and the password together, and the salt is stored in plain sight next to the result. Modern password hashing libraries generate the salt for you and encode it into the output string, so you usually never handle it directly.

Salts defeat two attacks:

  • Precomputed tables. Without salts, an attacker can build or download a giant table mapping common passwords to their hashes and look up stolen hashes instantly. That is the rainbow table attack. With a unique salt per user, a precomputed table would need to be rebuilt for every single salt, which is useless.
  • Batch cracking and duplicate detection. If two users share the password summer2020, unsalted hashes are identical, revealing that fact and letting one guess crack both. Salted hashes of the same password look completely different, so each account must be attacked separately.

A salt is not a secret. Its security comes from being unique and unpredictable, not hidden. That also means a salt does nothing if the attacker targets a single account: they have the salt and can grind guesses against that one hash at full speed.

What a pepper does

A pepper is a secret key, shared across all passwords, that is deliberately kept somewhere other than the user database: an environment-injected secret, a secrets manager, or ideally a hardware security module that performs the operation without ever exposing the key.

The threat it addresses is the common breach pattern where an attacker gets a copy of the database — through SQL injection, a leaked backup, or a misconfigured replica — but not the application servers or key store. With a pepper in place, the stolen hashes cannot be checked against guesses at all, because every guess also requires the pepper. The attacker has the lock but not the key.

A pepper only helps if it is actually stored separately. Putting it in a table in the same database, or in a config file that sits on the same compromised backup, gives you complexity without protection.

Salt vs pepper at a glance

SaltPepper
ScopeUnique per passwordOne value (or a few) for the whole system
Secret?No — stored with the hashYes — stored outside the database
GeneratedEvery time a password is setOnce, then rotated deliberately
DefeatsRainbow tables, batch cracking, duplicate detectionOffline cracking after a database-only leak
Handled by hashing libraryUsually automaticallyUsually by your own code
Cost if lostNothing; it is not secretAll passwords become unverifiable

How to apply a pepper correctly

There are two well-established patterns.

1. Pre-hash with an HMAC. Compute HMAC(pepper, password) and feed the result into Argon2 or bcrypt as if it were the password. An HMAC is a keyed hash, which is exactly what a pepper calls for. Encode the HMAC output (for example, base64) before passing it on. Some bcrypt implementations stop reading at a null byte and cap input length, so passing raw binary can silently truncate the effective password.

2. Encrypt the stored hash. Hash the password normally with its salt, then encrypt the resulting hash with a key held in a key management system before writing it to the database. On login, decrypt and verify. This has a practical advantage: you can rotate the key by decrypting and re-encrypting every stored hash, without needing users’ passwords.

Rotation is the hard part of pepper design. With the HMAC approach, the pepper is baked into the hash, so changing it means you can only re-hash a user’s password when they next log in. Store a key identifier next to each hash so you know which pepper version to use, and keep old versions available until every account has migrated.

Common mistakes

  • Reusing one salt for everyone. A global salt is just a weak pepper stored in the wrong place. Salts must be per-password.
  • Short or predictable salts. Usernames, user IDs, or timestamps are not salts. Use random bytes from a secure generator.
  • Storing the pepper next to the data. If the same breach that leaks the database also leaks the pepper, it provided no protection.
  • Using a fast hash because “the pepper makes it safe.” If the pepper ever leaks, you are left with fast, crackable hashes. Keep the slow hash regardless.
  • Comparing hashes with ordinary string equality. Use the library’s verify function or a constant-time comparison to avoid timing attacks.

Where this fits in the bigger picture

Even perfect storage does not stop an attacker who already has a user’s real password from another site. That threat — credential stuffing — is handled by rate limiting, breach-password checks, and multi-factor authentication. Longer term, passkeys remove the shared secret entirely, so there is no password hash to steal in the first place.

The takeaway

Salts and peppers solve different problems. A salt is public and unique per password, and it forces attackers to crack each hash individually. A pepper is secret and kept outside the database, and it makes a database-only leak useless for offline cracking. Use a slow password hashing function with its built-in salt as the non-negotiable baseline, then add a pepper held in a separate secret store — with a plan for rotation — when you want defense against the most common breach scenario.

Chisato Chisato · · 5 min read

Key Derivation Functions Explained: HKDF vs PBKDF2

A key derivation function turns secret input into cryptographic keys. When to use HKDF for high-entropy secrets, and PBKDF2, scrypt, or Argon2 for passwords.

#Security #Cryptography #Authentication
Chisato Chisato · · 5 min read

What Is a JWKS? JSON Web Key Sets Explained

A JWKS is a JSON document of public keys that lets any service verify JWT signatures. How JSON Web Key Sets work, the kid header, caching, and key rotation.

#Security #Authentication #Cryptography
Chisato Chisato · · 5 min read

What Is TOTP? How Time-Based One-Time Passwords Work

TOTP generates a new six-digit code every 30 seconds from a shared secret and the current time. How authenticator apps use it, and where it falls short.

#Security #Authentication #Cryptography