Password Hashing and Verification
Write hashpassword(password) and verifypassword(password, hashed) using bcrypt, and test verification with both a correct and an incorrect password.
Approach: hash the given password once with bcrypt.hashpw()/bcrypt.gensalt(), then check both a correct-attempt and a wrong-attempt string against that single hash with bcrypt.checkpw().
Input: Three lines: the real password, a "correct" login attempt, and a "wrong" login attempt.
Output: Two lines: "Correct attempt valid: True" and "Wrong attempt valid: False".
MySecurePassword123 MySecurePassword123 WrongPassword
Correct attempt valid: True Wrong attempt valid: False
- all three lines are non-empty strings with no newline
Hint 1
bcrypt.hashpw() and bcrypt.checkpw() both need bytes, not str — call .encode() on each password first.
Hint 2
bcrypt.checkpw(password.encode(), hashed) returns True only if that password, once hashed, matches the stored hash.
Hint 3
hashed already embeds the random salt bcrypt used, so checkpw() doesn't need the salt passed separately.
hashpassword produces a one-way, salted hash of the real password. verifypassword re-hashes a login attempt internally (via bcrypt.checkpw, which extracts the salt from the stored hash) and compares it against the stored hash, without ever needing to reverse the hash back into plain text — matching attempts return True, non-matching ones return False.