Bird
0
0
FastAPIframework~20 mins

Password hashing with bcrypt in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bcrypt Password Hashing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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}
AA string containing the hashed password
BA bytes object representing the hashed password
CA dictionary with salt and hash keys
DAn integer representing the hash value
Attempts:
2 left
💡 Hint
Think about what type the passlib CryptContext.hash method returns.
📝 Syntax
intermediate
2: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"
Apwd_context.verify(password, stored_hash)
Bpwd_context.hash(password) == stored_hash
Cbcrypt.checkpw(password, stored_hash)
Dpwd_context.check(password, stored_hash)
Attempts:
2 left
💡 Hint
Look for the method designed to verify passwords in passlib CryptContext.
🔧 Debug
advanced
2: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)
Apassword must be bytes, not string
Bstored_hash is bytes but should be a string
CCryptContext does not support bcrypt scheme
Dverify method requires async keyword
Attempts:
2 left
💡 Hint
Check the data types of the arguments passed to verify.
state_output
advanced
2: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)
ARaises a runtime error
BFalse
CNone
DTrue
Attempts:
2 left
💡 Hint
Consider if hashing the same password twice produces the same hash and if verify checks correctly.
🧠 Conceptual
expert
2: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?
ABecause bcrypt uses a different hashing algorithm each time
BBecause bcrypt hashes the password with a timestamp included
CBecause bcrypt uses a random salt each time to protect against rainbow table attacks
DBecause bcrypt truncates the password differently each time
Attempts:
2 left
💡 Hint
Think about what makes password hashes unique and secure.