Difficulty: Intermediate
Why do we add a salt to passwords before hashing them, and how should the salt be generated and stored?
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.
Suppose you store sha256(password). Then:
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.With a 16-byte random salt, the stored value becomes hash(salt + password). Now:
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 scheme | Same password, same hash? | Rainbow tables work? | Fast offline guessing? |
|---|---|---|---|
sha256(pw) | Yes | Yes | Yes |
sha256(salt + pw) | No | No | Yes |
bcrypt / argon2id | No | No | No, slowed by cost |
crypto.randomBytes, secrets.token_bytes, SecureRandom), not Math.random() or a timestamp.$argon2id$v=19$m=19456,t=2,p=1lt;salt>lt;hash>, so you need only one column.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.
| Unsalted SHA-256 | Salted hash | |
|---|---|---|
| Asha uses Password@123 | Hash ends ...9a1f | Salt 7Kq2, hash ends ...e40c |
| Ravi uses Password@123 | Hash ends ...9a1f | Salt Zp81, hash ends ...13b7 |
| Reveals shared password | Yes | No |
| Rainbow table lookup | Works | Useless |
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.
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.
Password Salt, Rainbow Tables, Password Hashing, Precomputation Attacks