📖 Scenario: You are building a simple Flask web app that needs to securely store user passwords. Instead of saving plain text passwords, you will use Werkzeug's password hashing utilities to protect user data.
🎯 Goal: Create a Flask app that hashes a user's password using Werkzeug's generate_password_hash function and then verifies it using check_password_hash.
📋 What You'll Learn
Create a Flask app instance
Import generate_password_hash and check_password_hash from werkzeug.security
Hash a plain text password using generate_password_hash
Verify the hashed password using check_password_hash
💡 Why This Matters
🌍 Real World
Web applications must never store plain text passwords. Using Werkzeug's hashing functions in Flask apps protects user data from leaks and hacks.
💼 Career
Understanding password hashing is essential for backend developers and security engineers working on user authentication systems.
Progress0 / 4 steps
1
Set up Flask app and import hashing functions
Create a Flask app by importing Flask from flask and instantiate it as app. Also import generate_password_hash and check_password_hash from werkzeug.security.
Flask
Hint
Remember to import both generate_password_hash and check_password_hash from werkzeug.security.
2
Create a plain text password variable
Create a variable called plain_password and set it to the string 'MySecret123'.
Flask
Hint
Assign the exact string 'MySecret123' to the variable plain_password.
3
Hash the plain text password
Create a variable called hashed_password and set it to the result of calling generate_password_hash with plain_password as the argument.
Flask
Hint
Use generate_password_hash with plain_password to create the hashed password.
4
Verify the hashed password
Create a variable called is_correct and set it to the result of calling check_password_hash with hashed_password and plain_password as arguments.
Flask
Hint
Use check_password_hash with hashed_password and plain_password to verify the password.
Practice
(1/5)
1. What is the main purpose of using generate_password_hash in a Flask app?
easy
A. To generate random passwords for users
B. To check if a password matches the stored hash
C. To encrypt the entire user database
D. To securely convert a plain password into a hashed version before storing
Solution
Step 1: Understand the role of generate_password_hash
This function takes a plain password and creates a hashed version that is safe to store.
Step 2: Differentiate from other functions
check_password_hash is 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 D
Quick Check:
Password hashing = secure storage [OK]
Hint: Hash passwords before storing to keep them safe [OK]
Common Mistakes:
Confusing hashing with encryption
Using check_password_hash to create hashes
Thinking hashing generates random passwords
2. Which of the following is the correct way to import the password hashing functions from Werkzeug?
easy
A. from flask import generate_password_hash, check_password_hash
B. import werkzeug.hash as wh
C. from werkzeug.security import generate_password_hash, check_password_hash
D. import werkzeug.security.hash_password
Solution
Step 1: Recall the correct import path
The password hashing functions are in werkzeug.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 C
Quick Check:
Correct import path = from werkzeug.security import generate_password_hash, check_password_hash [OK]
Hint: Import from werkzeug.security for hashing functions [OK]