Password hashing helps keep user passwords safe by turning them into secret codes that are hard to guess or reverse.
Password hashing with Werkzeug in Flask
from werkzeug.security import generate_password_hash, check_password_hash hashed_password = generate_password_hash('your_password') is_correct = check_password_hash(hashed_password, 'user_input_password')
generate_password_hash creates a safe hashed version of the password.
check_password_hash compares a hashed password with a user input to verify if they match.
hashed = generate_password_hash('mypassword')check_password_hash(hashed, 'mypassword') # returns True
check_password_hash(hashed, 'wrongpassword') # returns False
This program hashes a password, then checks if a user input matches it. It prints the hashed password and whether the input is correct.
from werkzeug.security import generate_password_hash, check_password_hash # Create a hashed password hashed_pw = generate_password_hash('secret123') print(f'Hashed password: {hashed_pw}') # Simulate user input user_input = 'secret123' # Check if user input matches the hashed password if check_password_hash(hashed_pw, user_input): print('Password is correct!') else: print('Password is incorrect!')
Never store plain text passwords; always hash them before saving.
Werkzeug uses strong hashing algorithms by default, so you don't need to choose one manually.
Hashed passwords include a random salt, so the same password will have different hashes each time.
Password hashing keeps passwords safe by turning them into secret codes.
Use generate_password_hash to hash passwords and check_password_hash to verify them.
Always hash passwords before storing to protect user data.
Practice
generate_password_hash in a Flask app?Solution
Step 1: Understand the role of
This function takes a plain password and creates a hashed version that is safe to store.generate_password_hashStep 2: Differentiate from other functions
check_password_hashis used to verify passwords, not generate them. Encryption of the whole database or generating random passwords are unrelated.Final Answer:
To securely convert a plain password into a hashed version before storing -> Option DQuick Check:
Password hashing = secure storage [OK]
- Confusing hashing with encryption
- Using check_password_hash to create hashes
- Thinking hashing generates random passwords
Solution
Step 1: Recall the correct import path
The password hashing functions are inwerkzeug.security, so import them directly from there.Step 2: Check each option
from werkzeug.security import generate_password_hash, check_password_hash correctly imports both functions. import werkzeug.hash as wh uses a wrong module alias. from flask import generate_password_hash, check_password_hash tries to import from Flask, which is incorrect. import werkzeug.security.hash_password uses a non-existent import path.Final Answer:
from werkzeug.security import generate_password_hash, check_password_hash -> Option CQuick Check:
Correct import path = from werkzeug.security import generate_password_hash, check_password_hash [OK]
- Importing from flask instead of werkzeug
- Using wrong module names or aliases
- Trying to import non-existent functions
from werkzeug.security import generate_password_hash, check_password_hash
hashed = generate_password_hash('mypassword')
print(check_password_hash(hashed, 'mypassword'))
print(check_password_hash(hashed, 'wrongpass'))Solution
Step 1: Understand the hashing and checking process
The password 'mypassword' is hashed. Thencheck_password_hashverifies if the given password matches the hash.Step 2: Evaluate the two checks
Checking with 'mypassword' returns True because it matches. Checking with 'wrongpass' returns False because it does not match.Final Answer:
True False -> Option AQuick Check:
Correct password = True, wrong password = False [OK]
- Assuming check_password_hash returns the hash
- Mixing up True and False results
- Not understanding that hashes are one-way
from werkzeug.security import generate_password_hash, check_password_hash
password = 'secret'
hash = generate_password_hash(password)
if check_password_hash(password, hash):
print('Password matches')Solution
Step 1: Review the function signature of check_password_hash
The correct order ischeck_password_hash(hashed_password, plain_password).Step 2: Identify the argument order in the code
The code passespasswordfirst andhashsecond, which is reversed and causes a logic error.Final Answer:
Arguments to check_password_hash are reversed -> Option BQuick Check:
check_password_hash(hashed, password) correct order [OK]
- Swapping arguments in check_password_hash
- Thinking generate_password_hash needs bytes input
- Forgetting to import check_password_hash
Solution
Step 1: Understand secure password storage
Passwords must be hashed before storing to protect user data from leaks.Step 2: Use correct verification method
Usecheck_password_hashto verify the plain password against the stored hash during login.Step 3: Eliminate incorrect options
Storing plain passwords is insecure. Hashing is one-way, so decrypting is impossible. Direct comparison of hash and plain password fails.Final Answer:
Hash passwords with generate_password_hash before storing, then verify with check_password_hash -> Option AQuick Check:
Hash before store, verify with check_password_hash [OK]
- Storing plain passwords
- Trying to decrypt hashes
- Comparing hash and plain password directly
Ready to prove it?
Pass the test and this pattern moves unknown
