Storing Passwords Safely
A website must never store passwords as plain text: a database leak would expose every account, and people reuse passwords. Storing a plain fast hash is not enough either, because two users with the same password get the same digest and attackers hash billions of guesses per second.
The right way:
- add a unique random salt to each password, so identical passwords give different digests;
- use a deliberately slow password hash (PBKDF2, scrypt, bcrypt or Argon2) so each guess costs time;
- store the salt and the digest, never the password.
To log in, hash the attempt with the stored salt and compare. Use a constant-time comparison, hmac.compare_digest, so timing does not leak information. The example uses the standard library's PBKDF2. Real sites use hundreds of thousands of iterations.
Why add a unique salt to each password?
With the fixed salt, print the first 16 hex characters of the PBKDF2-HMAC-SHA256 digest of the password 'letmein' using 1000 rounds (use .hex()[:16]).
Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.
Tip: use the left and right arrow keys to move between lessons.