Skip to content

Algorithms

provide.foundation.crypto.algorithms

TODO: Add module docstring.

Classes

Functions

get_digest_size

get_digest_size(algorithm: str) -> int

Get the digest size in bytes for an algorithm.

Parameters:

Name Type Description Default
algorithm str

Hash algorithm name

required

Returns:

Type Description
int

Digest size in bytes

Raises:

Type Description
ValidationError

If algorithm is not supported

Source code in provide/foundation/crypto/algorithms.py
def get_digest_size(algorithm: str) -> int:
    """Get the digest size in bytes for an algorithm.

    Args:
        algorithm: Hash algorithm name

    Returns:
        Digest size in bytes

    Raises:
        ValidationError: If algorithm is not supported

    """
    hasher = get_hasher(algorithm)
    return hasher.digest_size

get_hasher

get_hasher(algorithm: str) -> Any

Get a hash object for the specified algorithm.

Parameters:

Name Type Description Default
algorithm str

Hash algorithm name

required

Returns:

Type Description
Any

Hash object from hashlib

Raises:

Type Description
ValidationError

If algorithm is not supported

Source code in provide/foundation/crypto/algorithms.py
def get_hasher(algorithm: str) -> Any:
    """Get a hash object for the specified algorithm.

    Args:
        algorithm: Hash algorithm name

    Returns:
        Hash object from hashlib

    Raises:
        ValidationError: If algorithm is not supported

    """
    validate_algorithm(algorithm)

    algorithm_lower = algorithm.lower()

    # Handle special cases
    if algorithm_lower.startswith("sha3_"):
        # sha3_256 -> sha3_256 (hashlib uses underscores)
        return hashlib.new(algorithm_lower)
    if algorithm_lower.startswith("blake2"):
        # blake2b, blake2s
        return hashlib.new(algorithm_lower)
    # Standard algorithms (md5, sha1, sha256, etc.)
    return hashlib.new(algorithm_lower)

is_secure_algorithm

is_secure_algorithm(algorithm: str) -> bool

Check if an algorithm is considered cryptographically secure.

Parameters:

Name Type Description Default
algorithm str

Hash algorithm name

required

Returns:

Type Description
bool

True if algorithm is secure, False otherwise

Source code in provide/foundation/crypto/algorithms.py
def is_secure_algorithm(algorithm: str) -> bool:
    """Check if an algorithm is considered cryptographically secure.

    Args:
        algorithm: Hash algorithm name

    Returns:
        True if algorithm is secure, False otherwise

    """
    return algorithm.lower() in SECURE_ALGORITHMS

validate_algorithm

validate_algorithm(algorithm: str) -> None

Validate that a hash algorithm is supported.

Parameters:

Name Type Description Default
algorithm str

Hash algorithm name

required

Raises:

Type Description
ValidationError

If algorithm is not supported

Source code in provide/foundation/crypto/algorithms.py
def validate_algorithm(algorithm: str) -> None:
    """Validate that a hash algorithm is supported.

    Args:
        algorithm: Hash algorithm name

    Raises:
        ValidationError: If algorithm is not supported

    """
    if algorithm.lower() not in SUPPORTED_ALGORITHMS:
        raise ValidationError(
            f"Unsupported hash algorithm: {algorithm}",
            field="algorithm",
            value=algorithm,
            rule="must be one of: " + ", ".join(sorted(SUPPORTED_ALGORITHMS)),
        )