Password Storage
Hashing & Salting
Learn how modern systems protect your passwords using one-way hash functions and random salts so that even a database breach does not expose credentials.
Why Password Storage Matters
Every online service you use keeps a copy of your credentials. If an attacker compromises that database, the way passwords were stored determines whether millions of accounts are instantly exposed or remain protected.
What Is a Hash Function?
A cryptographic hash function takes any input and produces a fixed-length output called a digest. The process is one-way: you cannot reverse the digest back to the original input.
Properties of a Cryptographic Hash
The Problem with Plain Hashing
If you hash passwords without any extra steps, identical passwords produce identical digests. An attacker can pre-compute digests for millions of common passwords in a rainbow table and look up matches instantly.
What Is a Salt?
A salt is a random string generated uniquely for each user. It is prepended (or appended) to the password before hashing. The salt itself is stored alongside the hash in the database: it is not secret.
The Golden Rule of Password Storage
digest = hash(salt + password)
// To verify a login attempt:
if hash(stored_salt + attempt) == stored_digest:
grant access
Hashing vs Encryption
| Hashing | Encryption | |
|---|---|---|
| Direction | One-way | Two-way (reversible) |
| Key required? | No key needed | Requires a secret key |
| Output length | Fixed (e.g. 256 bits) | Variable (same as input) |
| Use case | Password verification | Protecting data in transit |
| Risk if DB stolen | Attacker must crack hashes | If key is stolen, all data exposed |
How to Store a Password Securely
Example: Registration Flow
Example: Login Verification
Salting in Action
Two users both choose the password "password123". With unique salts, their stored hashes are completely different:
salt_alice = "a1b2c3d4e5f6g7h8"
hash_alice = bcrypt("a1b2c3d4e5f6g7h8" + "password123")
= "$2b$12$Kj8L...xR4q"
// Bob
salt_bob = "z9y8x7w6v5u4t3s2"
hash_bob = bcrypt("z9y8x7w6v5u4t3s2" + "password123")
= "$2b$12$Nm3P...wT7d"
Why Use Slow Hash Functions?
Fast algorithms like SHA-256 can compute billions of hashes per second. Password-specific algorithms deliberately slow things down using a configurable work factor (also called cost or iterations).
Common Attacks on Stored Passwords
Common Mistakes
Summary Comparison
| Plaintext | Fast Hash | Salted + Slow Hash | |
|---|---|---|---|
| Storage | Raw password | Hash only | Salt + hash |
| Reversible? | Yes | No | No |
| Rainbow table | N/A | Vulnerable | Immune |
| Brute-force | N/A | Fast | Very slow |
| Industry standard? | ✕ | ✕ | ✓ |
Question 1 of 5
What is the primary purpose of adding a salt to a password before hashing?
Question 2 of 5
Which of the following is a one-way function, meaning you cannot recover the original input from the output?
Question 3 of 5
A company discovers that many users in their database share the same password hash. What is the most likely cause?
Question 4 of 5
Why are password-specific algorithms like bcrypt preferred over fast general-purpose hash functions like SHA-256?
Question 5 of 5
During login verification, the server needs to check if a user's password is correct. What does the server actually compare?