Explain the concept of Salt in password hashing.

Updated Apr 28, 2026

Short answer

A salt is a random value added to a password before hashing to make each password hash unique, even when users have the same password. It protects against attacks that use precomputed hash databases, such as rainbow tables, and makes large-scale password cracking more difficult. Salts are stored alongside password hashes because they are not meant to be secret.

Deep explanation

Password hashing is a security technique used to store passwords safely without storing the original password. Instead of saving:

TEXT
User Password:
MyPassword123

A system stores a hash:

TEXT
Password Hash:
ef92b778bafe771e89245b89ecbc08a44a4e166c06687...

When the user logs in, the system hashes the entered password and compares it with the stored hash.

However, storing only a normal hash creates a problem.

---

The Problem Without a Salt

Hash functions always produce the same output for the same input.

Example:

TEXT
Hash("password123")
=
ef92b778bafe771e89245b89ecbc08a44a4e166c06687...

If many users choose the same password:

TEXT
User A:
password123
User B:
password123
User C:
password123

Their stored hashes will be identical:

TEXT
User A → ef92b778...
User B → ef92b778...
User C → ef92b778...

An attacker who sees identical hashes can determine that multiple accounts use the same password.

Additionally, attackers can use precomputed tables containing common passwords and their hashes.

Example:

TEXT
password123 → ef92b778...
admin123 → 0192023a...
qwerty → 65e84be3...

If a stolen database contains matching hashes, attackers can quickly discover passwords.

---

How Salt Works

A salt is a random value generated for each password before hashing.

Instead of hashing:

TEXT
Password

The system hashes:

TEXT
Password + Salt

Example:

TEXT
Password:
password123
Salt:
a8F92kLm
Hash input:
password123a8F92kLm

The result:

TEXT
Hash:
8d72f91c4b2a...

Another user with the same password receives a different salt:

TEXT
Password:
password123
Salt:
Z91xPq44
Hash input:
password123Z91xPq44

Result:

TEXT
Hash:
b72ac9018d4e...

Although the passwords are the same, the hashes are different.

---

Salt Storage

A salt does not need to be hidden.

A database usually stores:

UsernameSaltPassword Hash
Alicea8F92kLm8d72f91c4b2a...
BobZ91xPq44b72ac9018d4e...

During login:

  1. The application retrieves the user's salt.
  2. It combines the entered password with that salt.
  3. It hashes the result.
  4. It compares the generated hash with the stored hash.

Example:

TEXT
Entered Password:
password123
Stored Salt:
a8F92kLm
Hash:
Hash(password123 + a8F92kLm)
Compare with stored hash

---

Benefits of Using Salts

Prevents Identical Passwords From Having Identical Hashes

Without salt:

TEXT
password123 → hash123
password123 → hash123

With salt:

TEXT
password123 + saltA → hashA
password123 + saltB → hashB

---

Protects Against Rainbow Tables

A rainbow table is a precomputed list of passwords and their hashes.

Without salts:

TEXT
Password → Hash
password123 → abc123

Attackers can look up the hash directly.

With salts:

TEXT
Password + Random Salt → Hash

Every possible salt creates a different hash, making precomputed tables impractical.

---

Increases Attack Cost

If an attacker steals a database containing password hashes, they must calculate guesses separately for each user because every password has a different salt.

Without salts:

TEXT
Try password123 once
Compare against millions of users

With salts:

TEXT
Try password123 + User1 salt
Try password123 + User2 salt
Try password123 + User3 salt

---

Salt vs Hashing vs Encryption

ConceptPurposeReversible
HashingStore passwords securelyNo
SaltMake password hashes uniqueNot applicable
EncryptionProtect data confidentialityYes with key

Passwords should generally be hashed, not encrypted, because the original password should never need to be recovered.

---

Password Hashing Algorithms With Salt Support

Modern password storage should use dedicated password hashing algorithms that include salt handling.

Common choices:

  • bcrypt
  • Argon2
  • scrypt
  • PBKDF2

Example:

TEXT
bcrypt(password, salt)
stored hash

These algorithms are intentionally slow and resource-intensive to make brute-force attacks harder.

---

Real-world example

A website stores user passwords.

A user registers:

TEXT
Username:
john@example.com
Password:
SecurePassword123

The application generates a random salt:

TEXT
Salt:
9xK4mP7q

It hashes:

TEXT
SecurePassword1239xK4mP7q

The database stores:

TEXT
Username:
john@example.com
Salt:
9xK4mP7q
Hash:
$2b$12$LQv3c1y9...

During login:

Python
import bcrypt
password = b"SecurePassword123"
stored_hash = b"$2b$12$LQv3c1y9..."
if bcrypt.checkpw(password, stored_hash):
print("Login successful")

The system verifies the password without ever storing the original password.

Common mistakes

  • * Storing plain text passwords instead of hashes.
  • * Using the same salt for every user.
  • * Treating a salt as a secret key.
  • * Using fast general-purpose hashes like SHA-256 directly for password storage.
  • * Reusing predictable or fixed salts.
  • * Believing that adding a salt makes weak passwords impossible to crack.
  • * Storing only the hash and losing the salt required for verification.
  • * Generating salts manually instead of using secure random functions.
  • * Encrypting passwords when hashing is the correct approach.

Follow-up questions

  • Why does every user need a different salt?
  • Should a salt be kept secret?
  • Why is hashing preferred over encryption for passwords?
  • What is the difference between a salt and a pepper?
  • Why are bcrypt and Argon2 preferred for password hashing?
  • Can a salted password hash still be cracked?

More Cryptography interview questions

View all →