Salting a password hash

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Salting a password hash

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a salt in password storage?
A A random value added to a password before it is hashed
B A secret key that lets you turn a hash back into the password
2. Two users both choose the password 'sunny123'. Their accounts use salting. What will their stored hashes look like?
A Identical, because the passwords are the same
B Different, because each user has a different salt
3. Which system gives each user their own salt?
A Each account gets a new random salt when the password is set
B One salt is shared by every user in the database
4. An attacker has a precomputed table of hashes for common passwords. How does salting affect it?
A The table still works, because salting only hides the username
B The table no longer matches, because the salt changes every hash
5. Which hash is better protected: hash('password') or hash('password' + 'k8Zq2x')?
A hash('password' + 'k8Zq2x'), because a salt is added
B hash('password'), because it is shorter and simpler

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1hash(password + salt) ACreates a new random salt for a user
2salt = random_string(16) BAdds a random salt to the password before hashing it
3Each user gets their own salt, so two users with the same password end up with different hashes CThe plain password is stored instead of the salted hash, so anyone who steals the database can read it.
4A salt defeats precomputed hash tables because the attacker cannot prepare hashes for every possible salt Duser1.salt != user2.salt
5salt = "abc" hash(password + salt) EThe salt is a fixed value, not random, so every user shares the same salt and identical passwords still give identical hashes.
6store(username, password, salt) Fhash("cat123" + "x9Qp") != hash("cat123")

Definitions

salt

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
storedHash ← hash(____ + password)
2.
salt ← ______(16) storedSalt ← salt
3.
hash1 ← hash("cat123" + salt1) hash2 ← hash("cat123" + _____)
4.
IF hash(attempt + salt) __ storedHash PRINT "Login OK"
5.
________ user IN users user.salt ← random(16) user.hash ← hash(user.pw + user.salt)

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A password ← "hunter2"
B salt ← "abc123"
C hash ← sha256(salt + password)
D store(user, salt, hash)
Question 2
A salt ← random_bytes(16)
B combined ← salt + password
C hash ← sha256(combined)
D save(user, hash)
Question 3
A row ← lookup(user)
B salt ← row.salt
C hash ← sha256(attempt)
D if hash = row.hash then
Question 4
A salt ← get_shared_salt()
B hash ← sha256(salt + password)
C store(user, salt, hash)
Question 5
A salt ← random_bytes(16)
B salted ← password
C hash ← sha256(salted)
D store(user, salt, hash)

Answer Key

Salting a password hash
Section A: Quick Questions
Q1
A — A random value added to a password before it is hashed
Q2
B — Different, because each user has a different salt
Q3
A — Each account gets a new random salt when the password is set
Q4
B — The table no longer matches, because the salt changes every hash
Q5
A — hash('password' + 'k8Zq2x'), because a salt is added
Section B: Match the Pairs
Q1
hash(password + salt) → Adds a random salt to the password before hashing it
Q2
salt = random_string(16) → Creates a new random salt for a user
Q3
Each user gets their own salt, so two users with the same password end up with different hashes → user1.salt != user2.salt
Q4
A salt defeats precomputed hash tables because the attacker cannot prepare hashes for every possible salt → hash("cat123" + "x9Qp") != hash("cat123")
Q5
salt = "abc" hash(password + salt) → The salt is a fixed value, not random, so every user shares the same salt and identical passwords still give identical hashes.
Q6
store(username, password, salt) → The plain password is stored instead of the salted hash, so anyone who steals the database can read it.
Section C: Definitions
Q1
salt — A random value added to a password before hashing, so identical passwords give different hashes. Each user gets their own salt, which defeats precomputed hash tables.
Section D: Fill in the Blanks
Q1
storedHash ← hash(salt + password)
Q2
salt ← random(16) storedSalt ← salt
Q3
hash1 ← hash("cat123" + salt1) hash2 ← hash("cat123" + salt2)
Q4
IF hash(attempt + salt) == storedHash PRINT "Login OK"
Q5
FOR EACH user IN users user.salt ← random(16) user.hash ← hash(user.pw + user.salt)
Section E: Spot the Error
Q1
B — A salt must be random. A fixed, guessable text like "abc123" can be built into an attacker's precomputed table. It should be salt ← random_bytes(16).
Q2
D — The salt must be saved with the hash. Without it the system can't repeat the hashing when the user logs in. It should be save(user, salt, hash).
Q3
C — At login the stored salt must be added to the attempt before hashing. Leaving it out gives a different hash, so the right password would fail. It should be sha256(salt + attempt).
Q4
A — Each user needs their own new random salt. One shared salt means identical passwords give identical hashes again. It should be salt ← random_bytes(16).
Q5
B — The salt has to be joined to the password before hashing. Here the salt is ignored, so the hash is unsalted. It should be salted ← salt + password.