Storing Passwords as Hashes
Why websites store a hash of your password instead of the password itself, how hashing works, and why salt is added.
1The problem with storing passwords
A login system has to check your password every time you sign in. The simplest approach is to store every user's password in a database and compare. But if an attacker ever steals that database (and databases do get stolen), they immediately know every user's password, and since people reuse passwords, every user's other accounts too.
Password-based systems should never store passwords in plaintext (readable form). Instead, they store a hash of each password.
2What a hash function is
A cryptographic hash function (also called a message digest function) is a mathematical algorithm that takes data of any length, processes it through a fixed set of steps, and outputs a fixed-length string of bits called the hash (also called a checksum, message digest, or digest). Well-known hash functions:
| Hash function | Notes |
|---|---|
| MD5 | Deprecated: no longer safe in security settings |
| SHA-1 | Deprecated |
| SHA-256, SHA-512 | SHA = Secure Hash Algorithm; widely used today |
| NTHash | Used by Windows to store passwords |
| RIPEMD-160 | Another established hash function |
Here are real SHA-256 hashes, written as 64 hexadecimal characters (256 bits):
SHA-256("cat")
= 77af778b51abd4a3c51c5ddd97204a9c3ae614ebccb75a606c3b6865aed6744e
SHA-256("The quick brown fox jumps over the lazy dog, and then it naps ...")
= 3eb67160e386f10bf463c879ab07bf3bc841656f605619a7becf4416259f7d6b
3Four properties of cryptographic hashes
| Property | Meaning | Why it matters for passwords |
|---|---|---|
| Collision resistant | It is difficult to find two different inputs that produce the same hash | An attacker cannot easily make up a different password that matches yours |
| Pre-image resistant | Given a hash, it is infeasible to work out the input that produced it | A stolen hash does not reveal the password |
| Repeatable | The same input always gives the same hash | The site can check your password by hashing it again and comparing |
| Fixed length | The output length never changes, whatever the input size | Every stored hash takes the same space; length reveals nothing about the password |
Change one character and the hash changes completely:
SHA-256("Sunflower7!") = d3b35b7f58fe2e6c663912722a06dfb1d3148140f0429b8c54f6689f0c470a43
SHA-256("Sunflower8!") = 1c12a79796eed5149a704134f78f6d2d5e1c9ba6c66dfd9698033f568fef2037
4How login works with hashes
When a user creates a password, the system hashes it and stores the hash. When the user logs in, the system hashes whatever they type and compares that hash with the stored one. If they match, the user is authenticated. The real password never has to be stored.
5Salt
Because hashing is repeatable, two users with the same password would have identical hashes. That is a problem: an attacker who cracks one immediately knows the other's password, and can spot every user with a popular password at a glance. To prevent this, a few random bits called salt are combined with each user's password before hashing. Each user gets a unique salt, so even identical passwords produce different hashes.
6Collisions and retired hash functions
An n-bit hash has 2n possible outputs. SHA-256 has 2256, about 1.16 × 1077 possibilities. That is enormous, but the number of possible inputs is infinite, so some different inputs must share a hash. Two inputs with the same hash is called a collision.
For a good hash function, collisions exist but are practically impossible to find. Adversaries try to find efficient ways to force collisions. When someone discovers one for a specific hash function, it is deprecated: no longer used in secure settings. MD5 and SHA-1 are deprecated for this reason.
Store hashes, not passwords. Hashing is one-way, repeatable, and fixed-length, so a site can check passwords without keeping them, and salt makes every user's hash unique.
7Worked examples
The stolen database
An attacker steals a website's user database. Compare what they get if passwords were stored in plaintext versus as salted SHA-256 hashes.
- 1Plaintext: every password is immediately readable. The attacker can log in as anyone and try those passwords on other sites.
- 2Salted hashes: the attacker sees only hashes and salts. Because hashes are pre-image resistant, they cannot be reversed.
- 3The attacker would have to guess passwords and hash each guess with each user's salt to find matches: slow work that strong passwords defeat.
Wrong by one character
A user's stored hash comes from Sunflower7!. They type Sunflower8!. What happens?
- 1The system hashes what was typed: SHA-256("Sunflower8!").
- 2As the log above shows, changing one character changes most of the hash.
- 3The new hash does not match the stored one, so login fails.
Two users, one password
Two employees both choose Sunflower7!. Without salt, what can an attacker with the database learn? With salt?
- 1Without salt: both rows show the same hash. The attacker sees they share a password, and cracking one cracks both.
- 2With salt: each has a different salt, so the stored hashes are completely different. Nothing shows they match, and each must be cracked separately.
A system still using MD5
An old system stores password hashes with MD5. Why should it be upgraded?
- 1MD5 has known efficient methods for forcing collisions, so it is deprecated.
- 2Deprecated functions are no longer trusted in secure settings.
- 3Upgrade to a current function (for passwords, one designed to be slow to compute, with salt), and re-hash passwords as users next log in.
8Common mistakes
Encryption is meant to be reversed with a key. Hashing is one-way: there is no key that turns a hash back into the password.
Salt is stored with the hash. Its job is to make each hash unique, not to be hidden.
Attackers who steal hashes can still guess common passwords, hash them, and compare. Hashing protects strong passwords far better than weak ones.
“The system stores a salted hash of each password instead of the password. At login, it hashes the entered password with the user's salt and compares the result to the stored hash. Because hashes are pre-image resistant, an attacker who steals the database cannot reverse them, and because each user has a unique salt, identical passwords produce different hashes.”
Name the property you rely on (pre-image resistance, repeatability, salt) and explain what it prevents.