01 / 21

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.

Cyber Security

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.

✕ Plaintext
Passwords stored as-is. One breach exposes every account instantly.
★ Encrypted
Reversible with a key. If the key leaks, all passwords are exposed.
✓ Hashed + Salted
One-way transformation. Even with the database, passwords stay hidden.

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.

Input Hash Function Fixed-Length Digest One-Way: Cannot Reverse

Properties of a Cryptographic Hash

1
Deterministic: The same input always produces the same output.
2
Pre-image resistant: Given a digest, it is computationally infeasible to find the original input.
3
Collision resistant: It is extremely hard to find two different inputs that produce the same digest.
4
Avalanche effect: Changing even one bit of input flips roughly half the output bits.

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.

USER PASSWORD STORED HASH Alice password123 ef92b7...a3c1 Bob password123 ef92b7...a3c1 Carol s3cur3Pa$ 7f1ab2...d8e4 ★ Alice and Bob have IDENTICAL hashes Attacker cracks one, gets both passwords

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.

Password + Salt Hash Digest user input random, unique stored in DB stored in DB

The Golden Rule of Password Storage

// Stored in the database:
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

1
Generate a random salt (at least 16 bytes) unique to this user.
2
Combine: prepend (or append) the salt to the user's password.
3
Hash with a slow algorithm: use bcrypt, scrypt, or Argon2: never plain SHA-256.
4
Store both the salt and the resulting digest in the database.
5
On login: retrieve the stored salt, hash the attempt the same way, and compare digests.

Example: Registration Flow

User enters: "MyP@ss99" Generate salt: "x9Fk2mQ7..." Combine: "x9Fk2mQ7...MyP@ss99" salt + password bcrypt() slow hash DATABASE RECORD salt: x9Fk2mQ7... hash: $2b$12$a4kL...

Example: Login Verification

User types: "MyP@ss99" DB Lookup: salt: x9Fk2mQ7... bcrypt( salt + attempt) New digest: $2b$12$a4kL... Stored digest: $2b$12$a4kL... ==? ✓ Match: Access Granted

Salting in Action

Two users both choose the password "password123". With unique salts, their stored hashes are completely different:

// Alice
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).

SHA-256
~10 billion hashes/sec on a modern GPU. Entire dictionary cracked in seconds.
bcrypt (cost 12)
~13 hashes/sec per core. A dictionary attack now takes years.

Common Attacks on Stored Passwords

Rainbow Table
Pre-computed lookup of hash-to-password pairs. Defeated by unique salts.
Dictionary Attack
Hashes every word in a dictionary. Slow hashing makes this impractical.
Brute Force
Tries every possible combination. Exponential time: strong passwords survive.

Common Mistakes

✕ Storing Plaintext
Storing passwords as-is means a single breach exposes all credentials immediately.
✕ Reusing Salts
Using the same salt for every user defeats its purpose: identical passwords still produce identical hashes.
✕ Using Fast Hashes
MD5 or SHA-256 are too fast. Billions of guesses per second make brute-force trivial.
✕ Encrypting Instead
Encryption is reversible. If the key leaks, every password is instantly readable.

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?