Cyberstudy
Supplementary — not from your PDF Intermediate ~30 min

See why passwords are salted and stretched

A short Python experiment showing how salting stops identical passwords producing identical hashes, and how key stretching makes each hash slower to compute.

Environment

Python 3 on your own computer (python in a terminal). No extra packages needed.

Before you start

  • Read Hashing (p.54) and Salting and Key Stretching (p.76).

You will

  • Compare unsalted and salted hashes
  • Measure the cost of PBKDF2 iterations

Steps

  1. 1

    Start Python and run: import hashlib, os, time.

  2. 2

    Hash the same made-up password twice without salt: hashlib.sha256(b'Sunset42!').hexdigest(). The results are identical.

  3. 3

    Add a random salt each time: salt = os.urandom(16); hashlib.sha256(salt + b'Sunset42!').hexdigest(). Run it twice; the hashes now differ.

  4. 4

    Time key stretching: t = time.time(); hashlib.pbkdf2_hmac('sha256', b'Sunset42!', salt, 600_000); print(time.time() - t).

  5. 5

    Repeat with 1,000 and then 600,000 iterations and compare the times.

  6. 6

    Write down why a site that stored unsalted, fast hashes would be a bigger problem after a breach than one using salted PBKDF2.

Check your understanding

  • ?What problem do precomputed (rainbow) tables solve for an attacker, and how does a salt remove it?
  • ?Does the salt need to be secret? Why or why not?
  • ?Why is making each hash slower a benefit for defenders?