"""
Password utility functions for user authentication.
"""

import bcrypt
from typing import Union


def verify_password(password: str, pw_hash: str) -> bool:
    """Verify a password against its hash."""
    try:
        return bcrypt.checkpw(
            bytes(password, encoding="utf-8"), bytes(pw_hash, encoding="utf-8")
        )
    except:
        return False


def encrypt_password(password: str) -> Union[bytes, None]:
    """Encrypt/hash a password using bcrypt."""
    try:
        return bcrypt.hashpw(bytes(password, encoding="utf-8"), bcrypt.gensalt())
    except:
        return None


# Legacy function names for backward compatibility
def get_password_hash(password: str) -> str:
    """Hash a password using bcrypt (legacy function name)."""
    encrypted = encrypt_password(password)
    if encrypted:
        return encrypted.decode('utf-8')
    return None
