Bird
0
0

Examine this Flask code snippet:

medium📝 Debug Q6 of 15
Flask - Security Best Practices
Examine this Flask code snippet:
from werkzeug.security import generate_password_hash, check_password_hash
password = 'mypassword'
hash = generate_password_hash(password)
if check_password_hash(hash, password):
    print('Access granted')

What is the issue with this code?
Agenerate_password_hash should not be used for passwords
BThe arguments to check_password_hash are reversed
CPassword should be hashed twice before checking
DThe password variable should be bytes, not string
Step-by-Step Solution
Solution:
  1. Step 1: Understand check_password_hash parameters

    The function check_password_hash expects the hashed password as the first argument and the plain password as the second.
  2. Step 2: Analyze the code

    The code passes password as the first argument and hash as the second, which is reversed.
  3. Final Answer:

    The arguments to check_password_hash are reversed -> Option B
  4. Quick Check:

    Correct argument order is (hashed_password, plain_password) [OK]
Quick Trick: check_password_hash(hashed, plain) not reversed [OK]
Common Mistakes:
MISTAKES
  • Swapping the order of arguments in check_password_hash
  • Hashing the password twice unnecessarily
  • Passing bytes instead of strings without encoding

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes