Performance: Password hashing with bcrypt
This affects server response time and user experience during authentication by adding CPU work for hashing passwords securely.
Jump into concepts and practice - no test required
import bcrypt password = b"user_password" hashed = bcrypt.hashpw(password, bcrypt.gensalt(rounds=12))
import bcrypt password = b"user_password" hashed = bcrypt.hashpw(password, bcrypt.gensalt(rounds=4))
| Pattern | CPU Load | Blocking | Security Strength | Verdict |
|---|---|---|---|---|
| Low cost factor (rounds=4) | Low CPU | Minimal blocking | Weak security | [X] Bad |
| Recommended cost factor (rounds=12) | Medium CPU | Moderate blocking | Strong security | [OK] Good |
| Synchronous verification | Medium CPU | Blocks event loop | Strong security | [X] Bad |
| Asynchronous verification | Medium CPU | Non-blocking | Strong security | [OK] Good |
bcrypt for password hashing in FastAPI?print(pwd_context.verify('secret123', hashed_password)) if hashed_password is generated by hashing 'secret123'?
from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
hashed_password = pwd_context.hash('secret123')
print(pwd_context.verify('secret123', hashed_password))from passlib.context import CryptContext
pwd_context = CryptContext(schemes=["bcrypt"])
password = "mypassword"
hashed = pwd_context.hash(password)
if pwd_context.verify(password, hashed):
print("Password verified")
else:
print("Verification failed")