Bird
0
0

Identify the error in this Flask password check code:

medium📝 Debug Q14 of 15
Flask - Security Best Practices
Identify the error in this Flask password check code:
from werkzeug.security import generate_password_hash, check_password_hash
hashed = generate_password_hash('mypassword')
if check_password_hash('mypassword', hashed):
    print('Access granted')
AThe hashed and plain password arguments are reversed in check_password_hash.
Bgenerate_password_hash should not be used for hashing passwords.
CThe print statement should be outside the if block.
DThere is no error; the code works correctly.
Step-by-Step Solution
Solution:
  1. Step 1: Check argument order in check_password_hash

    The first argument must be the hashed password, second is the plain password.
  2. Step 2: Identify the reversed arguments

    The code passes plain password first and hash second, causing a logic error.
  3. Final Answer:

    The hashed and plain password arguments are reversed in check_password_hash. -> Option A
  4. Quick Check:

    check_password_hash(hash, password) order matters [OK]
Quick Trick: Pass hashed first, plain password second to check_password_hash [OK]
Common Mistakes:
MISTAKES
  • Swapping arguments in check_password_hash
  • Using generate_password_hash incorrectly
  • Ignoring function argument order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes