Skip to content

Password Utilities

hash(password, password_pepper=None)

Hashes password with salt and pepper using Argon2id.

Parameters:

Name Type Description Default
password (SecretStr, required)

Password to hash.

required
password_pepper SecretStr

Pepper to hash password with. Defaults to None.

None

Returns:

Name Type Description
str str

Hashed password.

Source code in src/potato_util/crypto/password.py
@validate_call
def hash(password: SecretStr, password_pepper: SecretStr | None = None) -> str:
    """Hashes password with salt and pepper using Argon2id.

    Args:
        password        (SecretStr, required): Password to hash.
        password_pepper (SecretStr, optional): Pepper to hash password with. Defaults to None.

    Returns:
        str: Hashed password.
    """

    _ph = PasswordHasher()
    _seasoned_password = password.get_secret_value()
    if password_pepper:
        _seasoned_password = _seasoned_password + password_pepper.get_secret_value()

    _hash_password = _ph.hash(_seasoned_password)
    return _hash_password

verify(hashed_password, password, password_pepper=None)

Verifies password with salt and pepper against hashed password using Argon2id.

Parameters:

Name Type Description Default
hashed_password (str, required)

Hashed password.

required
password (SecretStr, required)

Raw password to verify.

required
password_pepper SecretStr

Pepper to verify password with. Defaults to None.

None

Returns:

Name Type Description
bool bool

True if password is match, False otherwise.

Source code in src/potato_util/crypto/password.py
@validate_call
def verify(
    hashed_password: str, password: SecretStr, password_pepper: SecretStr | None = None
) -> bool:
    """Verifies password with salt and pepper against hashed password using Argon2id.

    Args:
        hashed_password (str      , required): Hashed password.
        password        (SecretStr, required): Raw password to verify.
        password_pepper (SecretStr, optional): Pepper to verify password with. Defaults to None.

    Returns:
        bool: True if password is match, False otherwise.
    """

    _ph = PasswordHasher()
    _seasoned_password = password.get_secret_value()
    if password_pepper:
        _seasoned_password = _seasoned_password + password_pepper.get_secret_value()

    try:
        _ph.verify(hashed_password, _seasoned_password)
        return True
    except VerifyMismatchError:
        return False