Hashing, encoding and basic security · 11 / 15 Exercise Hard

Store passwords with salt

Objective

A password must never be stored as is or with a simple hash. What is stored is the result of a slow derivation function such as pbkdf2_hmac, mixed with a different salt per user, so two users with the same password have different fingerprints and precomputed tables are useless.

Instructions

  • The first line has the number n of commands: register user password salt or login user password (one word per field).
  • For each user store the salt and the derived fingerprint: start from salt + password (in UTF-8) and apply SHA-256 1000 times in a row, each time on the previous result (as bytes); store the hexadecimal. (In real programs use pbkdf2_hmac or scrypt, which are more secure; here it is stretched by hand because the in-browser grader does not include them.) Registering shows OK (or Error: user already exists if it already exists). Logging in shows Access granted or Access denied (if the user does not exist, it is also denied).
Input
4
register ana s3cret x1
login ana s3cret
login ana otra
login bob s3cret
Output
OK
Access granted
Access denied
Access denied

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise