What is password hashing?
Every time you make an account somewhere, the website has to solve a tricky problem: it needs some way to check your password every single time you log in, which means it needs to keep a record of it somewhere in its database. But storing your actual, readable password is dangerous. Databases get stolen — that's exactly what happens in a data breach — and if a database of plain-text passwords ever leaks, every single account in it is compromised instantly, no cracking required.
The fix almost every website uses is called hashing. A hash function is a piece of math that takes any input at all — a word, a password, an entire book — and turns it into a fixed-length, seemingly-random string of characters called a hash. Type in "hi" or type in the entire text of a novel, and you'll get back a string of the exact same length, and it will look like complete nonsense either way.
The one-way part is the whole point: a good hash function is easy to compute in one direction — password in, hash out — but computationally impractical to reverse. There's no "unhash" button. Given only the hash, even the website that created it cannot get your original password back out.
Here's an analogy that makes it click. Think about a blender.
Blending a banana and a strawberry into a smoothie is easy. But there is no process on Earth that un-blends that smoothie back into a whole banana and a whole strawberry — the information about their original shape is gone, scrambled together forever. Hashing your password works the same way. The website "blends" your password using a hash function, gets back a smoothie-like hash, and stores only that. Your actual password never sits in the database at all.
If it never stores your password, how does logging in still work?
This is the part that trips people up at first: if the website never keeps your actual password, how does it know when you type the right one? The answer is that it doesn't need your password to check your password — it only needs to compare hashes.
- When you create your account, the website hashes the password you chose and stores only that hash.
- Every time you log in, the website takes whatever you just typed, runs it through the exact same hash function, and gets a brand-new hash.
- Then it compares that brand-new hash to the one it already had stored. If the two hashes match exactly, you must have typed the same password — because, as you'll see below, even a one-character difference produces a completely different hash.
# This is the ONLY thing the website's database has stored: stored_hash = "ef92b778bafe771e89245b89ecbc08a44a4e166c06659911881f383d4473e94f" typed_password = input("Password: ") typed_hash = sha256(typed_password) if typed_hash == stored_hash: print("Welcome back!") else: print("Incorrect password.")
Password: ******** Welcome back!
Notice what's missing: nowhere in that program does the website ever "remember" your password in readable form. It never even holds onto typed_password after computing the hash. It only ever deals in hashes — which is exactly why a stolen database of hashes is so much less dangerous than a stolen database of real passwords.
Live hashing lab: try it yourself
This isn't a simulation — the box below runs a real hash function called SHA-256 (Secure Hash Algorithm, 256-bit), the same algorithm used all over the real internet, using your browser's own built-in cryptography engine. Type anything you want and watch the hash update, character by character, with every single keystroke.
computing…
Your browser doesn't support the Web Crypto API needed for this demo, but the ideas below still apply everywhere SHA-256 is used.
Try adding a single character, deleting one, or just changing capitalization. The hash doesn't get "a little different" — it becomes a completely different, unrecognizable string.
The avalanche effect: one tiny change, a totally different hash
computing…
computing…
Those two inputs differ by exactly one capital letter. Their hashes still end up sharing almost nothing in common. That's called the avalanche effect, and it's a required property of any good hash function: the smallest possible change to the input should completely scramble the output, so nobody can guess anything about your password by studying patterns in the hash.
Hashing vs. encryption
If you've been through Caesar and Vigenère ciphers or read about how HTTPS protects your data in transit, you've already met encryption — and it's easy to mix the two up, since both turn readable data into scrambled-looking gibberish. The difference is what happens next.
Encryption
- Two-way, on purpose. Something is encrypted so that it can be decrypted again later.
- Turning ciphertext back into the original message requires the right key.
- Used whenever someone needs the original data back — like your browser and codehs.com reading each other's traffic over HTTPS.
- If the key leaks, everything ever encrypted with it can be decrypted.
Hashing
- One-way, on purpose. A hash is never meant to be turned back into the original input — there is no key that reverses it.
- The same input always produces the same hash, but there's no "un-hashing" step, ever.
- Used when you only need to check something against the original, like a password, without ever needing to see that original again.
- Not even the website that created the hash can recover your password from it.
Curious about the math that makes some operations easy to do forward but hard to undo? See the modulus math behind encryption for more on that idea.
Why hashing alone isn't quite enough: salting
Hashing solves the "don't store readable passwords" problem, but plain hashing has a weakness of its own: the same input always produces the exact same hash. If two different users both pick the password "Password123", their stored hashes will be identical, character for character.
Attackers know this, so instead of attacking one account at a time, some of them build a rainbow table ahead of time: a giant, precomputed list pairing millions of common passwords with their hashes. If your stolen hash happens to be sitting in that table — which it will be, if your password is common or predictable — the attacker can look it up and instantly know your password, with no real "cracking" required at all.
The fix is called salting. Before hashing, the website generates a random, unique string for each user — the salt — and hashes it together with the password: hash(password + salt). The salt gets stored alongside the hash (it doesn't need to be secret, just unique per user). Now two users with the identical password end up with two completely different stored hashes, and a rainbow table built without knowing those specific random salts is useless against them.
Without salting
Same password ("Password123") → identical hash. Crack one, and you've cracked both — and a rainbow table finds this hash instantly.
With salting
Same password, unique random salts → two totally different hashes. Alice and Bob's precomputed rainbow-table entries don't apply here at all.
Hashing meets password strength
Hashing and salting both protect the password after it's already stored — but they don't make guessing impossible, just slower and less efficient. An attacker who steals a salted hash database hasn't given up. They can still take a guess, hash it with the stolen salt, and check for a match — over and over, extremely fast, using specialized hardware built for exactly this.
That's the moment where password strength stops being abstract advice and starts mattering directly. If your password is short, common, or predictable, it's one of the very first guesses tried. If it's long, random, and unique, the number of guesses required to stumble onto it becomes so astronomically large that even fast, dedicated cracking hardware would need longer than a human lifetime — sometimes longer than the age of the universe — to get there.
For the full math on why length and randomness matter so much more than clever substitutions like "p@ssw0rd", see How Strong Is Your Password? — and if you want a tool that generates a genuinely strong one for you, try the Safe Password Generator.
Check your understanding
In your own words: why is a stolen database of password hashes much less dangerous to users than a stolen database of the actual, plain-text passwords?
Your answer:
Sample answer: if the stolen database has real passwords in it, an attacker can read every single account's password immediately and log in anywhere that person reused it. But if the database only has hashes, the attacker can't just read the passwords off the page — hashing is a one-way process, so there's no way to compute the original password directly from its hash. The attacker is stuck guessing passwords one at a time and hashing each guess to check for a match, which (especially with salting, and especially against a long, unpredictable password) can take an impractically long time. The hashes limit the damage instead of handing every password over instantly.
Quick quiz
1. What makes a hash function "one-way"?
2. A website's password database gets stolen, but it only ever stored password hashes, never plain-text passwords. Why is this much less dangerous?
3. What problem does salting solve?
Keep learning about cybersecurity
You've now seen how websites protect your password by never actually storing it — turning a real cryptographic function into your account's first line of defense. Next, learn how the information you leave behind everywhere else online adds up into something called your digital footprint.
Part of the Cybersecurity Basics tutorial series.