Hash functions
How does a website know your password is correct without knowing your password? How does your phone notice that a downloaded file got damaged along the way? Both times with the same trick: a fingerprint of the data. Small, always the same size, and unique to whatever went in.
Words you might need
- Hash (or hash function)
- A calculator that you give something — a word, a photo, a whole book — and that always turns it into a string of characters of the same length. The same input always gives the same output. A different input, even just one letter, gives a completely different output.
- Bit
- The smallest piece of information: a 0 or a 1. Eight bits make a byte. If something is "256 bits", that's 256 zeros and ones in a row.
- SHA-256
- The name of the hash function most used today. The 256 is the number of bits in the output: always exactly 256, whether you feed in one letter or a whole movie. In hexadecimal that's 64 characters.
- Algorithm
- A fixed recipe of calculation steps. SHA-256, SHA-1 and MD5 are three different recipes with the same goal.
Three rules a hash sticks to
| Rule | What it means |
|---|---|
| One-way traffic | You can't calculate the input back from the hash. Not "hard", not "possible with a fast enough computer" — there simply is no way back. Like you can't get the cow back from a ground beef patty. |
| No collisions | Two different inputs shouldn't get the same hash. In theory it could happen (there are infinitely many texts and only 2256 hashes), but no one manages to find one. |
| Avalanche effect | One letter different, and roughly half of all the bits in the output flip. Nothing is left that resembles the previous hash. |
Try it yourself
Everything happens in your browser. Nothing is sent to the server.
- Click Make the fingerprint. You'll see four hashes of the same sentence, using four different recipes. Notice how long each one is.
- Change one letter in the sentence and click again. Compare. Do you still recognise anything?
- Click Show the avalanche effect: the demo changes one character itself and counts how many bits flip.
- Paste in a really long text — song lyrics, an essay. The hash stays the same length.
Why that length matters
256 bits means 2256 possible outcomes. That's a number with 78 digits. For comparison: the number of atoms in the universe is a number with roughly 80 digits. Every possible text gets a spot in a space as big as the universe — the chance that two different texts accidentally land on the same spot is practically zero.
The lesson you can't see
Look at the SHA-1 line in the demo. It looks exactly as random as SHA-256, just shorter. Yet SHA-1 is broken: in 2017, researchers from Google and CWI in Amsterdam showed two different PDF files with the exact same SHA-1 hash. The "no collisions" rule had been broken. MD5 had gone down much earlier.
You can't tell any of that from the output. Whether a hash function holds up doesn't depend on how random it looks, but on whether mathematicians have found a weak spot in it. That's why you use SHA-256 today and not something that "also looks fine".
Can you "decrypt" a hash? No. Nothing was encrypted, there's nothing to decrypt. Yet there are websites that claim they can — and sometimes they really do pull it off. How that's possible, and why it matters for your passwords, is the next chapter.
This is math: the birthday paradox
In a class of 23 students, the chance that two of them share a birthday is over 50%. That feels wrong — aren't there 365 days? — but it's correct, because you're not comparing 23 students against one date, you're comparing all 253 pairs of students against each other. That same little calculation decides how long a hash needs to be: with 2256 possibilities, you'd expect the first collision after roughly 2128 attempts. That's probability theory, and it's why 256, not 128, is the standard.