Skip to content

passwords

mlte/user/authentication.py

Handling of passwords.

hash_password(password)

Gets the hash of a given plain password.

Source code in mlte/user/passwords.py
19
20
21
22
23
24
def hash_password(password: str) -> str:
    """Gets the hash of a given plain password."""
    pwd_bytes = password.encode("utf-8")
    salt = bcrypt.gensalt()
    hashed_password = bcrypt.hashpw(password=pwd_bytes, salt=salt)
    return hashed_password.decode("utf-8")

verify_password(plain_password, hashed_password)

Verifies that a plain password matches a hashed one.

Source code in mlte/user/passwords.py
10
11
12
13
14
15
16
def verify_password(plain_password: str, hashed_password: str) -> bool:
    """Verifies that a plain password matches a hashed one."""
    password_byte_enc = plain_password.encode("utf-8")
    hashed_password_byte_enc = hashed_password.encode("utf-8")
    return bcrypt.checkpw(
        password=password_byte_enc, hashed_password=hashed_password_byte_enc
    )