File size: 776 Bytes
bc1cd44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import bcrypt
from utils.logger import Logger

log = Logger()


def hash_password(plain_password: str) -> str:
    """
    Hash a plaintext password using bcrypt.
    """
    try:
        hashed = bcrypt.hashpw(plain_password.encode("utf-8"), bcrypt.gensalt())
        return hashed.decode("utf-8")
    except Exception as exc:
        log.error(f"Password hashing failed: {exc}")
        raise


def verify_password(plain_password: str, hashed_password: str) -> bool:
    """
    Verify a plaintext password against its bcrypt hash.
    """
    try:
        return bcrypt.checkpw(
            plain_password.encode("utf-8"), hashed_password.encode("utf-8")
        )
    except Exception as exc:
        log.error(f"Password verification failed: {exc}")
        return False