"""Password hashing utilities using bcrypt directly.""" import asyncio import bcrypt def hash_password(password: str) -> str: """Hash a password using bcrypt.""" return bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt()).decode("utf-8") def verify_password(plain_password: str, hashed_password: str) -> bool: """Verify a password against its hash.""" return bcrypt.checkpw(plain_password.encode("utf-8"), hashed_password.encode("utf-8")) async def hash_password_async(password: str) -> str: """Hash a password using bcrypt (non-blocking). Wraps the blocking bcrypt operation in a thread pool to avoid blocking the event loop during password hashing. """ return await asyncio.to_thread(hash_password, password) async def verify_password_async(plain_password: str, hashed_password: str) -> bool: """Verify a password against its hash (non-blocking). Wraps the blocking bcrypt operation in a thread pool to avoid blocking the event loop during password verification. """ return await asyncio.to_thread(verify_password, plain_password, hashed_password)