Spaces:
Runtime error
Runtime error
| """Password hashing and verification""" | |
| import bcrypt | |
| def hash_password(password: str) -> str: | |
| """ | |
| Hash a plain text password using bcrypt | |
| Args: | |
| password: Plain text password | |
| Returns: | |
| Hashed password as string | |
| """ | |
| # Bcrypt has a 72-byte limit, truncate if necessary | |
| password_bytes = password.encode('utf-8') | |
| if len(password_bytes) > 72: | |
| password_bytes = password_bytes[:72] | |
| # Generate salt and hash | |
| salt = bcrypt.gensalt() | |
| hashed = bcrypt.hashpw(password_bytes, salt) | |
| # Return as string | |
| return hashed.decode('utf-8') | |
| def verify_password(plain_password: str, hashed_password: str) -> bool: | |
| """ | |
| Verify a plain text password against a hashed password | |
| Args: | |
| plain_password: Plain text password | |
| hashed_password: Hashed password to verify against | |
| Returns: | |
| True if password matches, False otherwise | |
| """ | |
| # Truncate to 72 bytes to match hashing behavior | |
| password_bytes = plain_password.encode('utf-8') | |
| if len(password_bytes) > 72: | |
| password_bytes = password_bytes[:72] | |
| # Convert hashed password to bytes if it's a string | |
| if isinstance(hashed_password, str): | |
| hashed_password = hashed_password.encode('utf-8') | |
| return bcrypt.checkpw(password_bytes, hashed_password) | |