Hashing: how websites check your password without knowing it
Every time you log in, and every time someone checks whether a downloaded file was tampered with
What problem does it solve?
How can a website check your password if it's too dangerous for them to store it?
The big idea
A hash function is a blender for data: easy to blend, practically impossible to un-blend, and the same ingredients always give the same smoothie. The site stores only the hash of your password, and when you log in it hashes what you typed and compares. Change one letter and about half the output bits flip (the 'avalanche'), so similar passwords look nothing alike. A random 'salt' added per user stops attackers from using pre-computed tables, and deliberately slow hashes make guessing billions of passwords painfully expensive.
How it works, step by step
- 1. When you sign up, the site makes a random salt for you.
- 2. It stores the salt and H(salt + password), never the password itself.
- 3. When you log in, it recomputes H(salt + what you typed).
- 4. If the hashes match, you're in; if the database leaks, attackers get only scrambled outputs.
The key formula
- a one-way hash function, e.g. SHA-256, or a deliberately slow password hash like bcrypt or Argon2
- glue the two strings together
- random extra characters, different for each user, stored in plain sight
How fast is it?
SHA-256 has 2²⁵⁶ possible outputs, about 10⁷⁷, so finding two inputs with the same hash by luck is hopeless. For passwords, fast is bad: password hashes like Argon2 (winner of the 2015 Password Hashing Competition) are designed to be slow and memory-hungry on purpose.
Try it yourself
- Why is 'password123' unsafe even with hashing? (Attackers hash common passwords first.)
- If a hash had only 1000 possible outputs, how many random inputs before two collide? Look up the birthday paradox: it's about 40, not 500.
Fun fact
A blockchain is mostly just a hash chain: each block stores the hash of the one before, so changing old history would change every hash after it. Stuart Haber and Scott Stornetta invented hash-chained timestamps in 1991 (Bitcoin's whitepaper cites them), and their company published the latest hash in the New York Times classified ads. Bitcoin adds 'proof of work': miners hunt for a number that makes the block's hash start with many zeros, which uses a lot of electricity on purpose.