AP Cybersecurity · Study guide

Storing Passwords as Hashes

AP® Cybersecurity · Unit 4 · Device Security, Malware & Authentication

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 functionNotes
MD5Deprecated: no longer safe in security settings
SHA-1Deprecated
SHA-256, SHA-512SHA = Secure Hash Algorithm; widely used today
NTHashUsed by Windows to store passwords
RIPEMD-160Another established hash function

Here are real SHA-256 hashes, written as 64 hexadecimal characters (256 bits):

Short input, long input: same length output
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

PropertyMeaningWhy it matters for passwords
Collision resistantIt is difficult to find two different inputs that produce the same hashAn attacker cannot easily make up a different password that matches yours
Pre-image resistantGiven a hash, it is infeasible to work out the input that produced itA stolen hash does not reveal the password
RepeatableThe same input always gives the same hashThe site can check your password by hashing it again and comparing
Fixed lengthThe output length never changes, whatever the input sizeEvery stored hash takes the same space; length reveals nothing about the password

Change one character and the hash changes completely:

One character different: 62 of 64 hex characters changed
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.

Figure 1. Because hashing is repeatable, the same password always produces the same hash, so the site can check a password by comparing hashes.

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.

Figure 2. The salt is stored next to the hash; it is not secret. Its job is to make every user's hash unique, even when passwords are the same.

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.

The key idea

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

Worked Example 1 Why hash

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.

  1. 1
    Plaintext: every password is immediately readable. The attacker can log in as anyone and try those passwords on other sites.
  2. 2
    Salted hashes: the attacker sees only hashes and salts. Because hashes are pre-image resistant, they cannot be reversed.
  3. 3
    The attacker would have to guess passwords and hash each guess with each user's salt to find matches: slow work that strong passwords defeat.
Answer: Hashing turns an instant disaster into slow, often unsuccessful guessing work.
Worked Example 2 Login check

Wrong by one character

A user's stored hash comes from Sunflower7!. They type Sunflower8!. What happens?

  1. 1
    The system hashes what was typed: SHA-256("Sunflower8!").
  2. 2
    As the log above shows, changing one character changes most of the hash.
  3. 3
    The new hash does not match the stored one, so login fails.
Answer: Login fails. Even a one-character difference produces a completely different hash.
Worked Example 3 Why salt

Two users, one password

Two employees both choose Sunflower7!. Without salt, what can an attacker with the database learn? With salt?

  1. 1
    Without salt: both rows show the same hash. The attacker sees they share a password, and cracking one cracks both.
  2. 2
    With salt: each has a different salt, so the stored hashes are completely different. Nothing shows they match, and each must be cracked separately.
Answer: Salt hides matching passwords and forces the attacker to work on each account separately.
Worked Example 4 Deprecated functions

A system still using MD5

An old system stores password hashes with MD5. Why should it be upgraded?

  1. 1
    MD5 has known efficient methods for forcing collisions, so it is deprecated.
  2. 2
    Deprecated functions are no longer trusted in secure settings.
  3. 3
    Upgrade to a current function (for passwords, one designed to be slow to compute, with salt), and re-hash passwords as users next log in.
Answer: MD5 is deprecated because collisions can be forced. Use a current, salted password-hashing method.

8Common mistakes

Common Mistake 1 · Calling hashing “encryption”

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.

Common Mistake 2 · Thinking salt is secret

Salt is stored with the hash. Its job is to make each hash unique, not to be hidden.

Common Mistake 3 · Thinking hashes make weak passwords safe

Attackers who steal hashes can still guess common passwords, hash them, and compare. Hashing protects strong passwords far better than weak ones.

Explain password hashing like this

“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.