Why password hashes break

A wall full of archive drawers with one drawer open, containing two matching cards.

You read it all the time: "database of website X stolen, millions of passwords leaked". But a good site doesn't store your password — only its hash. And you can't calculate a password back from a hash, you know that since the last chapter. So… is there even a problem? Yes. And in this chapter you play the hacker to see why.

Words you might need

Database
The big table where a website keeps all its users: name, email address, and (if it's done right) the hash of the password.
Lookup table (or "rainbow table")
A list someone made in advance: on the left, billions of commonly used passwords; on the right, their hash. Anyone who comes across a hash looks it up on the right and reads off the password on the left. No calculating, just looking up.
Salt
Literally "salt": a random bit of text the website glues onto your password before it hashes it. A different one for every user. It's not secret — it's just stored next to the hash — but it makes every lookup table worthless.
PBKDF2
A hash function that's deliberately slow: it repeats the calculation hundreds of thousands of times. For you, that's half a second when logging in. For a hacker trying to guess billions of passwords, it's a wall.

How an "uncrackable" hash gets cracked anyway

The hacker doesn't calculate backwards. He calculates forwards. He takes a list of the billion most-used passwords — 123456, qwerty, welcome, the names of every football club — and calculates the hash of each one. That takes a while, but he only has to do it once. After that, he compares the stolen hashes against his list. Every hash that matches is a cracked password. No math, just patience and a big hard drive.

Play the hacker

Everything happens in your browser. Nothing is sent to the server. Even so, never type a password you actually use here — that's just a good habit.

  1. Click Hash and look up. The demo hashes your password and looks up the hash in a little table of a few dozen commonly used passwords. Found it? Then you're "cracked".
  2. Try qwerty, football, doctor. Then try something you make up yourself.
  3. Click Same password, with salt. Same password, hashed twice — and yet two completely different hashes. Look them up: nothing.
  4. Click With PBKDF2 (slow) and watch the time. Work out what that means for someone who has to try ten million times.

Salt: everyone gets their own problem

A hash is just a function, and you can write it down like any other. Call your password x and the hash y:

y = SHA256(x)

And that's exactly where the problem sits. The same x always gives the same y — that's the first rule from chapter 2. So everyone in the world who picks welcome ends up in the database with the exact same 64 characters:

UserPasswordHash in the database
Samwelcome280d44ab1e9f…
Noorwelcome280d44ab1e9f…
Juleswelcome280d44ab1e9f…

Three times the same row. The hacker looks up 280d44ab1e9f… once in his table and has all three at the same time. With ten million accounts it works exactly as well.

What salt changes about that

Salt fixes this by gluing one thing in front of the password:

y = SHA256(salt + x)

Exactly how you'd write it yourself. Watch the + though: here it doesn't mean adding up, but sticking together. Salt and password become one long text, and that is what the hash is taken of. A salt of a3f9c2e1… with password welcome therefore hashes the text a3f9c2e1…welcome.

And here's the point: every user gets a different salt, which the website draws at random the moment you register. Same table, same password:

UserSaltWhat gets hashedHash in the database
Sama3f9c2e1…a3f9c2e1…welcomeae637cc78b28…
Noor7c21e04a…7c21e04a…welcome2e3482b2342a…
Julesf40d8b37…f40d8b37…welcome3f573f0fc865…

Three times the same password, three hashes that have nothing to do with each other. That's the avalanche effect from chapter 2 at work: the input differs, so the output differs completely. From the database you can no longer see who has the same password.

But the salt is sitting right there

Look at that table again. The salt is in its own column, readable, in the same database that got stolen. So the hacker has it too. And it still works — that's the clever part.

His ready-made table holds the hashes of SHA256(x). What he needs are the hashes of SHA256(a3f9c2e1… + x). Those are completely different numbers. His table is worthless in one stroke: he has to compute it all over again — a billion passwords × one hash — and then he only has Sam. For Noor he starts from scratch, because her salt is different. And for Jules once more.

That's what salt does. It doesn't make your password one bit stronger. It turns one big job — cracking ten million accounts — into ten million separate jobs. Click Same password, with salt in the demo above and you'll see it happen twice with real numbers.

Slowness: the one weapon that really counts

SHA-256 is blazing fast — a decent graphics card manages billions of hashes per second. That's handy for files and disastrous for passwords. That's why good sites use a function that's deliberately slow. Click PBKDF2 in the demo and watch the time. A few hundred milliseconds is barely noticeable to you when logging in. But for the hacker, every attempt gets multiplied by that number — and he has billions of attempts to make.

What this means for you

For when you build a website yourself one day: never write this yourself. Every programming language has a built-in function that handles salt and slowness properly (in PHP it's called password_hash()). Rolling your own thing with a hash and a salt is the classic beginner mistake — and behind half the leaks in the news.

This is math: how big is big?

An 8-lowercase-letter password has 268 ≈ 200 billion possibilities. Sounds like a lot — a graphics card is through it in a minute. Four random words from a dictionary of 5000 words: 50004 = 625 trillion, and that's without any capital letters or digits. That's the difference between exponentiation with a large base and a large exponent. Whoever has a feel for big numbers designs better locks than whoever doesn't.