Challenge - 5 Problems
Bcrypt Password Hashing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this FastAPI bcrypt password hashing snippet?
Consider this FastAPI endpoint that hashes a password using bcrypt. What will be the type of the returned hashed password?
FastAPI
from fastapi import FastAPI from passlib.context import CryptContext app = FastAPI() pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") @app.post("/hash") async def hash_password(password: str): hashed = pwd_context.hash(password) return {"hashed_password": hashed}
Attempts:
2 left
💡 Hint
Think about what type the passlib CryptContext.hash method returns.
✗ Incorrect
The pwd_context.hash method returns a string that contains the hashed password including the salt and algorithm info. It is not bytes or a dictionary.
📝 Syntax
intermediate2:00remaining
Which option correctly verifies a password against a bcrypt hash in FastAPI?
Given a stored bcrypt hash and a plain password, which code snippet correctly verifies if the password matches the hash?
FastAPI
from passlib.context import CryptContext pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") stored_hash = "$2b$12$KIXQJ7Q1Y6b6xQ9v1zQO6u5v1Q0xQ0xQ0xQ0xQ0xQ0xQ0xQ0xQ0xQ" password = "mysecret"
Attempts:
2 left
💡 Hint
Look for the method designed to verify passwords in passlib CryptContext.
✗ Incorrect
The verify method of CryptContext checks if the plain password matches the stored hash securely. Option A compares strings incorrectly. Option A is invalid because bcrypt.checkpw expects bytes, not strings. Option A is not a valid method.
🔧 Debug
advanced2:00remaining
Why does this FastAPI bcrypt password verification raise a TypeError?
This code snippet raises a TypeError when verifying a password. What is the cause?
FastAPI
from passlib.context import CryptContext pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") stored_hash = b"$2b$12$KIXQJ7Q1Y6b6xQ9v1zQO6u5v1Q0xQ0xQ0xQ0xQ0xQ0xQ0xQ0xQ0xQ" password = "mysecret" result = pwd_context.verify(password, stored_hash)
Attempts:
2 left
💡 Hint
Check the data types of the arguments passed to verify.
✗ Incorrect
The verify method expects both password and hash as strings. Passing bytes as stored_hash causes a TypeError.
❓ state_output
advanced2:00remaining
What is the value of 'is_valid' after running this FastAPI bcrypt verification code?
Given the following code, what will be the value of the variable 'is_valid'?
FastAPI
from passlib.context import CryptContext pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto") password = "secret123" hash1 = pwd_context.hash(password) hash2 = pwd_context.hash(password) is_valid = pwd_context.verify(password, hash1) and (hash1 != hash2)
Attempts:
2 left
💡 Hint
Consider if hashing the same password twice produces the same hash and if verify checks correctly.
✗ Incorrect
Hashing the same password twice produces different hashes due to random salt. verify returns True for the correct password. So is_valid is True.
🧠 Conceptual
expert2:00remaining
Which statement best explains why bcrypt hashes differ each time even for the same password?
Why does hashing the same password multiple times with bcrypt produce different hash strings each time?
Attempts:
2 left
💡 Hint
Think about what makes password hashes unique and secure.
✗ Incorrect
Bcrypt generates a new random salt for each hash to ensure that identical passwords have different hashes, improving security.
