Spaces:
Sleeping
Sleeping
| import bcrypt | |
| def hash_password(password): | |
| """ | |
| Hash a password using bcrypt. | |
| Args: | |
| password (str): The plain text password to hash | |
| Returns: | |
| bytes: The hashed password | |
| """ | |
| # Convert string to bytes and generate a salt | |
| salt = bcrypt.gensalt() | |
| # Hash the password with the salt | |
| hashed = bcrypt.hashpw(password.encode('utf-8'), salt) | |
| return hashed | |
| def check_password(password, hashed_password): | |
| """ | |
| Check if a provided password matches the hashed password. | |
| Args: | |
| password (str): The plain text password to check | |
| hashed_password (bytes): The previously hashed password | |
| Returns: | |
| bool: True if password matches, False otherwise | |
| """ | |
| try: | |
| # Check if the password matches the hash | |
| return bcrypt.checkpw(password.encode('utf-8'), hashed_password) | |
| except Exception: | |
| # Return False if there's any error (e.g., invalid hash) | |
| return False | |