Skip to content

Password Utilities

hash(password, password_salt, password_pepper)

Hashes password with salt and pepper using Argon2id.

Parameters:

Name Type Description Default
password (SecretStr, required)

Password to hash.

required
password_salt (SecretStr, required)

Salt to hash password with.

required
password_pepper (SecretStr, required)

Pepper to hash password with.

required

Returns:

Name Type Description
str str

Hashed password.

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

    Args:
        password        (SecretStr, required): Password to hash.
        password_salt   (SecretStr, required): Salt to hash password with.
        password_pepper (SecretStr, required): Pepper to hash password with.

    Returns:
        str: Hashed password.
    """

    _ph = PasswordHasher()
    _seasoned_password = (
        password.get_secret_value()
        + password_salt.get_secret_value()
        + password_pepper.get_secret_value()
    )
    _hash_password = _ph.hash(_seasoned_password)
    return _hash_password

verify(hashed_password, password, password_salt, password_pepper)

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_salt (SecretStr, required)

Salt to verify password with.

required
password_pepper (SecretStr, required)

Pepper to verify password with.

required

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_salt: SecretStr,
    password_pepper: SecretStr,
) -> 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_salt   (SecretStr, required): Salt to verify password with.
        password_pepper (SecretStr, required): Pepper to verify password with.

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

    _ph = PasswordHasher()
    _seasoned_password = (
        password.get_secret_value()
        + password_salt.get_secret_value()
        + password_pepper.get_secret_value()
    )

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