Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the main purpose of using bcrypt for password hashing?
Bcrypt securely hashes passwords to protect them from being easily read or cracked if the database is compromised. It adds salt and uses a slow hashing algorithm to make attacks harder.
Click to reveal answer
beginner
In FastAPI, which Python library is commonly used to implement bcrypt password hashing?
The passlib library is commonly used with FastAPI to handle bcrypt hashing easily and securely.
Click to reveal answer
beginner
What does the term 'salt' mean in password hashing with bcrypt?
A salt is a random value added to the password before hashing. It ensures that even if two users have the same password, their hashes will be different.
Click to reveal answer
intermediate
How do you verify a password against a bcrypt hash in FastAPI?
You use the verify method from the hashing library (like passlib) to check if the plain password matches the stored bcrypt hash.
Click to reveal answer
intermediate
Why is bcrypt considered better than simple hashing functions like MD5 or SHA1 for passwords?
Bcrypt is designed to be slow and includes salting, which makes it resistant to brute force and rainbow table attacks, unlike fast hashes like MD5 or SHA1.
Click to reveal answer
What does bcrypt add to passwords before hashing to make them more secure?
APepper
BSalt
CPlain text
DCompression
✗ Incorrect
Bcrypt adds a random salt to each password before hashing to ensure unique hashes.
Which Python library is commonly used with FastAPI for bcrypt hashing?
Asqlalchemy
Brequests
Cflask
Dpasslib
✗ Incorrect
Passlib provides easy bcrypt hashing and verification functions.
Why is bcrypt hashing slower than MD5 or SHA1?
ATo make brute force attacks harder
BBecause it uses more memory
CTo save CPU resources
DBecause it compresses data
✗ Incorrect
Bcrypt is intentionally slow to make guessing passwords by brute force much harder.
How do you check if a user's password matches the stored bcrypt hash in FastAPI?
AUse the verify method from passlib
BCompare plain text passwords
CUse SQL queries
DHash the password with MD5
✗ Incorrect
The verify method safely compares the plain password with the stored bcrypt hash.
What happens if two users have the same password but bcrypt is used with salt?
APasswords get stored in plain text
BTheir hashes will be the same
CTheir hashes will be different
DPasswords get rejected
✗ Incorrect
Salt ensures that even identical passwords produce different hashes.
Explain how bcrypt protects passwords and why it is preferred over simple hashing methods.
Think about what makes bcrypt hashes different and safer.
You got /4 concepts.
Describe the steps to hash and verify a password using bcrypt in a FastAPI application.
Consider the flow from user input to password check.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using bcrypt for password hashing in FastAPI?
easy
A. To speed up the login process by caching passwords
B. To encrypt passwords so they can be decrypted later
C. To securely store passwords by converting them into a hashed format
D. To generate random passwords for users automatically
Solution
Step 1: Understand password hashing purpose
Password hashing converts passwords into a secure format that cannot be reversed, protecting user data.
Step 2: Identify bcrypt role in FastAPI
bcrypt is used to hash passwords securely, not to encrypt or cache them.
Final Answer:
To securely store passwords by converting them into a hashed format -> Option C
Quick Check:
Password hashing = secure storage [OK]
Hint: Hashing hides passwords, not encrypts or caches them [OK]
2. Which of the following is the correct way to import and create a bcrypt password context using passlib in FastAPI?
easy
A. from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
B. import bcrypt
pwd_context = bcrypt.PasswordContext()
C. from fastapi.security import bcrypt
pwd_context = bcrypt.Context()
D. import passlib
pwd_context = passlib.bcrypt()
Solution
Step 1: Recall correct import for bcrypt context
Passlib's CryptContext is imported from passlib.context and configured with schemes=["bcrypt"].
Step 2: Check syntax correctness
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") correctly imports and creates pwd_context with bcrypt scheme and deprecated="auto".
Final Answer:
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") -> Option A
Quick Check:
Correct import and setup = from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") [OK]
Hint: Use CryptContext from passlib.context with schemes=['bcrypt'] [OK]
Common Mistakes:
Importing bcrypt directly instead of CryptContext
Using wrong module names like fastapi.security
Calling non-existent constructors
3. Given the following code snippet, what will be the output of print(pwd_context.verify('secret123', hashed_password)) if hashed_password is generated by hashing 'secret123'?
A. Using verify method incorrectly with arguments reversed
B. No error; code works correctly
C. Not importing bcrypt module explicitly
D. Missing deprecated="auto" in CryptContext initialization
Solution
Step 1: Check CryptContext initialization
Best practice is to include deprecated="auto" to handle scheme deprecation warnings.
Step 2: Verify method usage and imports
verify is used correctly with (plain, hashed). bcrypt import is not needed explicitly with passlib.
Final Answer:
Missing deprecated="auto" in CryptContext initialization -> Option D
Quick Check:
Include deprecated="auto" to avoid warnings [OK]
Hint: Always add deprecated="auto" in CryptContext [OK]
Common Mistakes:
Omitting deprecated="auto" causes warnings
Reversing arguments in verify method
Importing bcrypt separately when unnecessary
5. You want to create a FastAPI endpoint that accepts a user's plain password, hashes it with bcrypt, and stores it securely. Which of the following code snippets correctly implements this functionality considering best practices?
hard
A. from fastapi import FastAPI
from passlib.context import CryptContext
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@app.post("/register")
async def register(password: str):
hashed_password = pwd_context.hash(password)
# Store hashed_password securely
return {"msg": "User registered"}
C. from fastapi import FastAPI
from passlib.context import CryptContext
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"])
@app.post("/register")
async def register(password: str):
hashed_password = pwd_context.hash(password.encode())
return {"msg": "Password hashed"}
D. from fastapi import FastAPI
from passlib.context import CryptContext
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@app.post("/register")
async def register(password: bytes):
hashed_password = pwd_context.hash(password)
return {"msg": "User registered"}
Solution
Step 1: Check correct use of passlib CryptContext and hashing
from fastapi import FastAPI
from passlib.context import CryptContext
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@app.post("/register")
async def register(password: str):
hashed_password = pwd_context.hash(password)
# Store hashed_password securely
return {"msg": "User registered"} correctly imports CryptContext with deprecated="auto" and hashes the plain string password.
Step 2: Validate FastAPI endpoint and parameter types
from fastapi import FastAPI
from passlib.context import CryptContext
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@app.post("/register")
async def register(password: str):
hashed_password = pwd_context.hash(password)
# Store hashed_password securely
return {"msg": "User registered"} uses async def with password as str, which is standard for FastAPI input. It hashes and comments storing securely.
Step 3: Compare other options for errors
from fastapi import FastAPI
import bcrypt
app = FastAPI()
@app.post("/register")
def register(password: str):
hashed_password = bcrypt.hashpw(password, bcrypt.gensalt())
return {"hashed": hashed_password} uses bcrypt module incorrectly with str instead of bytes; from fastapi import FastAPI
from passlib.context import CryptContext
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"])
@app.post("/register")
async def register(password: str):
hashed_password = pwd_context.hash(password.encode())
return {"msg": "Password hashed"} hashes password.encode() but misses deprecated="auto"; from fastapi import FastAPI
from passlib.context import CryptContext
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@app.post("/register")
async def register(password: bytes):
hashed_password = pwd_context.hash(password)
return {"msg": "User registered"} expects bytes input which is unusual for FastAPI JSON input.
Final Answer:
from fastapi import FastAPI
from passlib.context import CryptContext
app = FastAPI()
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
@app.post("/register")
async def register(password: str):
hashed_password = pwd_context.hash(password)
# Store hashed_password securely
return {"msg": "User registered"} -> Option A
Quick Check:
Use passlib CryptContext with str input and deprecated="auto" [OK]
Hint: Use passlib CryptContext with str password and deprecated="auto" [OK]
Common Mistakes:
Using bcrypt module directly with wrong input types
Omitting deprecated="auto" in CryptContext
Accepting password as bytes instead of str in FastAPI