Lesson 6: Hashing: How Passwords Are Stored Safely
A responsible website never stores your password as plain text, because a breach would then hand every password to the attacker. Instead, it stores a hash, a fixed-length string produced by running the password through a mathematical function that is easy to compute in one direction and practically impossible to reverse. When you log in, the site hashes the password you typed and compares it with the stored hash, and if they match, you are in, all without the site ever knowing your actual password. This lesson explains how hashing works, why a pinch of randomness called a salt is essential, and why a slow hash is a good hash, which together explain why some breaches are disasters and others are merely embarrassing.
What a hash is
A hash function takes any input, of any length, and produces a fixed-length output that looks random. Change one letter of the input and the output changes completely, which is called the avalanche effect, and the same input always produces the same output, so the site can check your password by hashing it again and comparing. But a hash cannot be reversed: knowing the output does not let you work out the input, except by guessing inputs one by one and hashing them until one matches, which is the only attack available, and the whole defence rests on making that guessing too slow to be worth it. A worked example with a toy hash, not a real one: imagine a function that turns the word apple into 7f3a and banana into 9c21, where the numbers look unrelated to the words, and you can see why the site can verify a password without storing it.
Salts: making every hash unique
Hashing alone has a weakness: two people with the same password produce the same hash, which tells an attacker that those two accounts share a password, and it lets attackers pre-compute hashes for common passwords once and compare them against every breach, using tables called rainbow tables. The fix is a salt, a random string generated for each account and stored alongside the hash, which is mixed into the password before hashing, so the same password produces a different hash for every user. A salt does not need to be secret, because its job is uniqueness, not secrecy, and it makes pre-computed tables useless, since the attacker would have to start over for every account. When you read about a breach, the first question to ask is whether the passwords were salted, because unsalted hashes of common passwords can be reversed in minutes, while salted ones resist for far longer.
Slow hashes and modern practice
The second fix is to make the hash slow on purpose. A fast hash is good for checking files, but bad for passwords, because an attacker can try billions of guesses per second, so password hashing uses deliberately slow algorithms, such as bcrypt, scrypt or Argon2, which take a fraction of a second each, unnoticeable to a real user logging in, but ruinous to an attacker trying millions of passwords. Modern practice is therefore salted, slow hashing, with a work factor that can be increased as computers get faster, and that is the standard a responsible site should meet. Old algorithms such as MD5 and SHA-1 are too fast for passwords and should never be used for them, even though they are still fine for checking whether a file has changed, which is a different job with different requirements.
What this means for you
You cannot see how a site stores your password, but you can still use this knowledge. First, it explains why a site can email you a password reset link but cannot email you your password: if it could, it would mean the password is stored in a reversible way, which is a bad sign. Second, it explains why a breach of salted, slow hashes is less urgent than a breach of plain-text passwords, though you should still change the password, because a weak password can be guessed even from a good hash. Third, it explains why your password's strength still matters: a hash can only slow an attacker down, and a password such as 123456 will be guessed in the first few tries whatever the algorithm. Hashing protects you, but it is your password that does the real work.
Key Terms
- Hash
- A fixed-length string produced from a password by a one-way function, stored instead of the password itself.
- Hash function
- A mathematical function that is easy to compute in one direction and practically impossible to reverse.
- Salt
- A random string mixed into a password before hashing, so the same password gives a different hash for each user.
- Rainbow table
- A pre-computed table of hashes for common passwords, used to reverse unsalted hashes quickly.
- bcrypt / Argon2
- Deliberately slow hashing algorithms designed for passwords.
- Avalanche effect
- The property that changing one letter of the input changes the hash output completely.
Discussion Questions
- Why can a good website email you a password reset link but not your actual password?
- If two users have the same password, why should their stored hashes be different?
- Why is a slow hash good for passwords but bad for checking files?
Practice Questions
- Why does a website store a hash instead of your password?
- What does a salt do?
- Why are password hashes deliberately slow?
- What is a rainbow table?
- Why is MD5 a poor choice for storing passwords?
Show answers
- So a breach does not hand attackers the passwords. The site can verify your password by hashing it again and comparing, without ever storing the password itself.
- It makes the same password produce a different hash for each user. A random string per account is mixed in before hashing, which defeats pre-computed rainbow tables.
- To make guessing too slow to be worth it. A fraction of a second is unnoticeable to a real user but ruinous to an attacker trying millions of passwords.
- A pre-computed table of hashes for common passwords. It lets an attacker reverse unsalted hashes quickly by looking them up instead of guessing.
- Because it is too fast. An attacker can try billions of MD5 guesses per second, while a slow algorithm such as bcrypt or Argon2 makes guessing far harder.