The Purpose of Salt

Difficulty: Intermediate

Question

Why do we add a salt to passwords before hashing them, and how should the salt be generated and stored?

Answer

A salt is a random value, unique per password, that is combined with the password before hashing. It ensures two users with the same password get different hashes, and it makes precomputed attacks like rainbow tables useless because an attacker would need a separate table for every possible salt. The salt is not a secret; it is stored alongside the hash and only needs to be random and unique.

The problem without salt

Suppose you store sha256(password). Then:

  • Every user with Password@123 has the exact same hash, so cracking one cracks all of them, and the attacker can see at a glance which accounts share passwords.
  • The attacker can use a rainbow table or a simple lookup table: hashes of billions of common passwords computed once, in advance, and reused against every leaked database. Cracking becomes a lookup instead of computation.
  • Hashes from one leaked site can be matched against another site's leak.

What the salt changes

With a 16-byte random salt, the stored value becomes hash(salt + password). Now:

  • Same passwords produce different hashes because the salts differ.
  • Precomputed tables are useless; a table built without your salt never matches. Building a table per salt is pointless because each salt is used once.
  • The attacker must attack each hash individually, so cracking a million-user database costs about a million times more than cracking one hash.

What salt does not do

Salt does not slow down a guess against a single hash. With a fast hash like SHA-256, an attacker can still try billions of guesses per second against one user's salted hash. That is why salt alone is not enough: you also need a deliberately slow, tunable algorithm such as Argon2id, bcrypt, scrypt or PBKDF2. These all handle salt for you.

Storage schemeSame password, same hash?Rainbow tables work?Fast offline guessing?
sha256(pw)YesYesYes
sha256(salt + pw)NoNoYes
bcrypt / argon2idNoNoNo, slowed by cost

How to generate and store salt correctly

  • Use a cryptographically secure random generator (crypto.randomBytes, secrets.token_bytes, SecureRandom), not Math.random() or a timestamp.
  • Use at least 16 bytes (128 bits).
  • Make it unique per password, and generate a new salt whenever the password changes.
  • Store it next to the hash. Modern algorithms embed it in the output string, for example $argon2id$v=19$m=19456,t=2,p=1
    lt;salt>
    lt;hash>
    , so you need only one column.
  • Do not use the username or email as the salt; they are predictable, reused across sites, and do not change on password reset.

If you want a secret component as well, that is a pepper, which is kept outside the database and complements the salt rather than replacing it.

Two users, same password. Without salt the hashes match; with salt they look unrelated.
Unsalted SHA-256Salted hash
Asha uses Password@123Hash ends ...9a1fSalt 7Kq2, hash ends ...e40c
Ravi uses Password@123Hash ends ...9a1fSalt Zp81, hash ends ...13b7
Reveals shared passwordYesNo
Rainbow table lookupWorksUseless

Code examples

Seeing the effect of salt (for illustration only)

import crypto from 'node:crypto';

const sha = (s) => crypto.createHash('sha256').update(s).digest('hex').slice(0, 16);

// Unsalted: identical
console.log('asha', sha('Password@123'));
console.log('ravi', sha('Password@123'));

// Salted: different
for (const user of ['asha', 'ravi']) {
  const salt = crypto.randomBytes(16).toString('hex');
  console.log(user, salt.slice(0, 8), sha(salt + 'Password@123'));
}

// In real code, use a slow hash that salts for you:
// await argon2.hash(password)  or  await bcrypt.hash(password, 12)

Salted SHA-256 removes identical hashes and rainbow tables but is still too fast for password storage. Production code should use Argon2id or bcrypt, which generate and embed the salt.

Salted PBKDF2 in Python when Argon2 is not available

import hashlib, hmac, secrets

ITERATIONS = 600_000  # OWASP guidance for PBKDF2-HMAC-SHA256

def hash_password(pw: str) -> str:
    salt = secrets.token_bytes(16)
    dk = hashlib.pbkdf2_hmac("sha256", pw.encode(), salt, ITERATIONS)
    return f"pbkdf2_sha256${ITERATIONS}${salt.hex()}${dk.hex()}"

def verify(pw: str, stored: str) -> bool:
    _, iters, salt_hex, dk_hex = stored.split("
quot;) dk = hashlib.pbkdf2_hmac("sha256", pw.encode(), bytes.fromhex(salt_hex), int(iters)) return hmac.compare_digest(dk.hex(), dk_hex) h = hash_password("Password@123") print(verify("Password@123", h), verify("wrong", h))

The salt and iteration count are stored in the same string as the hash, and compare_digest avoids timing leaks during verification.

Key points

  • Salt is random, unique per password, at least 16 bytes, and not secret
  • It defeats rainbow tables and hides which users share a password
  • Salt does not slow down guessing one hash; you still need a slow algorithm
  • Argon2id, bcrypt, scrypt and PBKDF2 generate and embed the salt automatically
  • Never use the username or email as the salt

Common follow-up questions

  • If the salt is stored in the database, why does it still help when the database leaks?
  • What is the difference between a salt and a pepper?
  • Why is salted SHA-256 still not good enough for passwords?
  • How would you migrate unsalted hashes to a modern scheme?

Interview tips

  • Answer the 'salt is public, so why does it help' question before the interviewer asks it
  • Separate the two jobs clearly: salt stops precomputation, a slow hash stops brute force
  • Mention that you would never implement salting manually in production; you would use Argon2id or bcrypt

Concepts covered

Password Salt, Rainbow Tables, Password Hashing, Precomputation Attacks

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