Storing passwords safely protects users from hackers. It keeps their accounts and data secure.
Password storage best practices in Flask
from werkzeug.security import generate_password_hash, check_password_hash # To store a password securely hashed_password = generate_password_hash('user_password') # To check a password during login is_correct = check_password_hash(hashed_password, 'user_password')
generate_password_hash creates a safe hashed version of the password.
check_password_hash compares a stored hash with a password input.
hashed = generate_password_hash('mypassword123') print(hashed)
check_password_hash(hashed, 'mypassword123') # returns True
check_password_hash(hashed, 'wrongpassword') # returns False
This Flask app lets users register and login. Passwords are stored safely as hashes. Login checks the password against the stored hash.
from flask import Flask, request, jsonify from werkzeug.security import generate_password_hash, check_password_hash app = Flask(__name__) users = {} @app.route('/register', methods=['POST']) def register(): username = request.json.get('username') password = request.json.get('password') if username in users: return jsonify({'message': 'User already exists'}), 400 hashed = generate_password_hash(password) users[username] = hashed return jsonify({'message': 'User registered successfully'}) @app.route('/login', methods=['POST']) def login(): username = request.json.get('username') password = request.json.get('password') hashed = users.get(username) if not hashed or not check_password_hash(hashed, password): return jsonify({'message': 'Invalid username or password'}), 401 return jsonify({'message': 'Login successful'}) # To run the app: # flask run # Example usage: # POST /register with JSON {"username": "alice", "password": "secret123"} # POST /login with JSON {"username": "alice", "password": "secret123"}
Never store plain text passwords. Always hash them before saving.
Use a strong hashing function like the one in Werkzeug (Flask's helper library).
Do not try to create your own hashing method; use trusted libraries.
Always hash passwords before storing them to keep user data safe.
Use generate_password_hash and check_password_hash from Werkzeug in Flask.
Never store or log plain passwords anywhere in your app.
Practice
Solution
Step 1: Understand password security risks
Storing plain text passwords means if someone hacks the database, they get all user passwords directly.Step 2: Importance of hashing
Hashing transforms passwords into unreadable strings, protecting user data even if the database leaks.Final Answer:
Because plain text passwords can be easily stolen if the database is compromised. -> Option AQuick Check:
Password safety = Avoid plain text storage [OK]
- Thinking hashing is optional
- Believing plain text is safe if database is secure
- Confusing hashing with encryption
Solution
Step 1: Recall Werkzeug's hashing function
Werkzeug providesgenerate_password_hashto hash passwords securely.Step 2: Identify correct function usage
The correct function call isgenerate_password_hash(password), other options are invalid or non-existent.Final Answer:
hashed = generate_password_hash(password) -> Option BQuick Check:
Use generate_password_hash() to hash passwords [OK]
- Using non-existent functions like hash_password
- Confusing hashing with encryption functions
- Trying to hash passwords manually
from werkzeug.security import generate_password_hash, check_password_hash
hashed = generate_password_hash('secret123')
result = check_password_hash(hashed, 'secret123')
print(result)Solution
Step 1: Understand generate_password_hash output
This function creates a hashed string from 'secret123'.Step 2: Check password correctness with check_password_hash
It compares the hashed password with the plain text 'secret123' and returns True if they match.Final Answer:
True -> Option DQuick Check:
check_password_hash returns True if passwords match [OK]
- Expecting the original password as output
- Confusing True/False return values
- Assuming check_password_hash modifies the hash
from werkzeug.security import generate_password_hash, check_password_hash
hashed = generate_password_hash('mypassword')
if check_password_hash('mypassword', hashed):
print('Access granted')Solution
Step 1: Check argument order in check_password_hash
The first argument must be the hashed password, second is the plain password.Step 2: Identify the reversed arguments
The code passes plain password first and hash second, causing a logic error.Final Answer:
The hashed and plain password arguments are reversed in check_password_hash. -> Option AQuick Check:
check_password_hash(hash, password) order matters [OK]
- Swapping arguments in check_password_hash
- Using generate_password_hash incorrectly
- Ignoring function argument order
Solution
Step 1: Understand secure password storage
Passwords must be hashed before storing to prevent exposure if database leaks.Step 2: Verify passwords securely on login
Usecheck_password_hashto compare the stored hash with the login password safely.Final Answer:
Hash passwords with generate_password_hash before storing, then verify with check_password_hash during login. -> Option CQuick Check:
Hash before store, verify with check_password_hash [OK]
- Storing or logging plain passwords
- Using reversible encryption instead of hashing
- Comparing plain passwords directly
Ready to prove it?
Pass the test and this pattern moves unknown
