In Flask applications, why is it important to store passwords as salted hashes instead of plain hashes?
Think about how attackers try to guess passwords using pre-made lists.
Salting adds unique random data to each password before hashing. This stops attackers from using precomputed hash tables (rainbow tables) to reverse hashes quickly.
What will be the output of this Flask code snippet when checking a password?
from werkzeug.security import generate_password_hash, check_password_hash hashed = generate_password_hash('mypassword') result = check_password_hash(hashed, 'wrongpassword') print(result)
Check what happens when the password does not match the hash.
The function check_password_hash returns False if the password does not match the stored hash.
Which code snippet correctly hashes a password using werkzeug.security in Flask?
Look for the recommended secure hashing method in werkzeug.
The default and recommended method is pbkdf2:sha256. Options like salt or rounds are not valid parameters.
Consider this Flask code snippet:
from werkzeug.security import generate_password_hash, check_password_hash
hashed = generate_password_hash('secret')
if check_password_hash('secret', 'secret'):
print('Access granted')
else:
print('Access denied')Why does it always print 'Access denied'?
Check the order and meaning of arguments in check_password_hash.
The first argument to check_password_hash must be the hashed password string. Passing the plain password causes the check to fail.
Given this Flask code:
from werkzeug.security import generate_password_hash
hash1 = generate_password_hash('mypassword')
hash2 = generate_password_hash('mypassword')
print(hash1 == hash2)What will be printed and why?
Think about how salting affects the output of hashing.
Each call to generate_password_hash creates a new random salt, so even the same password produces different hashes. This improves security by preventing hash comparison attacks.