Authentication & Hashing
Security Fundamentals · Security Fundamentals track
5 min read
In this module
Proving You Are Who You Claim to Be
Encryption keeps data secret but says nothing about who sent it. Authentication is the process of verifying identity — confirming that a user, device, or message is what it claims to be. The most common form is the humble password, and what makes storing passwords safely possible is the hash function.
What Is a Hash Function?
A cryptographic hash function takes an input of any size and produces a fixed-size output (the hash or digest). Good cryptographic hash functions have these properties:
- Deterministic - the same input always produces the same output.
- One-way - you cannot reconstruct the input from the output.
- Avalanche effect - changing even one bit of the input produces a dramatically different hash.
- Collision resistant - it's infeasible to find two different inputs with the same hash. MD5 and SHA-1 lost this property, which is why they're retired for security use.
Common algorithms include SHA-256 (which produces a 256-bit hash) and bcrypt (designed specifically for passwords). They're built for different jobs. SHA-256 is meant to be fast, and a single GPU can try billions of SHA-256 guesses a second, so a fast hash is the wrong tool for storing passwords even with a salt.
Try the avalanche effect on real SHA-256 below. Type any message, flip a single bit of one character, and count how many of the 256 output bits change. It lands near half every time, with no visible pattern in which ones.
Flipping the lowest bit of that character gives Ce Bitwise. One input bit is different.
Hashing…
Encryption is reversible - you can get the original data back with the right key. Hashing is a one-way operation - there is no key, and you cannot recover the original input from the hash. This is exactly what makes hashing suitable for password storage: the server never needs to know your actual password, only whether what you typed produces the same hash.
How Login Systems Work
A secure login system never stores your password in plain text. Instead:
- When you create an account, the server hashes your password and stores the hash.
- When you log in, the server hashes what you typed and compares it to the stored hash.
- If the hashes match, you are authenticated.
If an attacker steals the database, they get hashes — not passwords. Since hashing is one-way, they can't simply run it backwards.
The Problem: Rainbow Tables
An attacker could precompute the hashes of millions of common passwords and store them in a lookup table. To crack a stolen hash, they just look it up. A rainbow table is a cleverer version of the same idea that stores chains of hashes instead of every pair, trading some lookup time for far less disk, but the effect is the same: the work is done once, before any database is stolen, and reused against every one.
The Solution: Salting
A salt is a random string generated for each user and prepended to the password before hashing. Even if two users choose the same password, their different salts produce entirely different hashes. This makes rainbow tables useless because the attacker would need a separate table for every possible salt - a computationally impossible task.
Modern password hashing algorithms like bcrypt, scrypt, and Argon2 handle salting automatically and are deliberately slow (Argon2 and scrypt are memory-hungry too, which blunts GPUs), making brute-force attacks expensive. Compare the stored and computed hashes in constant time, and remember that even good hashing only protects stolen hashes; multi-factor authentication is what protects accounts whose passwords leak some other way.
Worked Example: Storing and Checking One Password
Follow the password password (a bad choice, which is the point) through three ways of storing it.
Unsalted SHA-256. The server stores
5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8Every site that does this stores exactly the same 64 hex characters for that password. Paste them into a search engine and the answer comes up within seconds. Two users who both picked password get identical rows in the database, so cracking one cracks both.
Salted SHA-256. Give Alice the random salt 9f2c41d7 and Bob 7b0e88a3. Hashing salt plus password gives
Alice: SHA-256("9f2c41d7password") = cb027db14f54d31bc3c8a8cf4661d30dde45def794d62d06d1c87630e400211aBob: SHA-256("7b0e88a3password") = 222aff3401f1c34b6c86d41cf217bd166d7b7004f58a5e9cda47657ba522501aPrecomputed tables are now useless and the two rows look unrelated. But SHA-256 is still fast, so an attacker with the database can simply try a wordlist against each salt at billions of guesses a second, and password falls on the first try.
bcrypt. Here's a real bcrypt hash of the same password, made with htpasswd -bnBC 12 "" password:
$2y$12$6L45DPXSTE/b8AT.BuY34OLDpxigfzd2hqK0AP8DVvODBvq9fmH1yIt's 60 characters with its settings built in. $2y$ names the bcrypt variant (most libraries write $2b$; same algorithm). 12 is the cost: the expensive setup step runs 212 = 4,096 times, and each extra point doubles the work. The next 22 characters, 6L45DPXSTE/b8AT.BuY34O, are the salt, and the last 31 are the hash itself. OWASP's Password Storage Cheat Sheet currently suggests Argon2id first, and a bcrypt cost of at least 10 where bcrypt is what you have.
Predict: the server stores only that 60-character string. When Alice logs in, how does it know which salt to use, and doesn't storing the salt right next to the hash defeat the point?
It reads the salt straight out of the string. Verification splits off the variant, cost and salt, runs bcrypt on what Alice typed with those same settings, and compares the result with the stored hash in constant time. Salts aren't secrets. Their job is to make every user's hash unique so that nothing can be precomputed and no two accounts can be cracked together, and they do that just as well when the attacker can read them. A secret value kept outside the database, called a pepper, is a separate and optional extra layer.