Skip to content

gcages.hashing#

Support for hash calculations

Functions:

Name Description
get_file_hash

Get the hash of a file

get_file_hash #

get_file_hash(
    fp: Path,
    algorithm: str = "sha256",
    buffer_size: int = 64 * 1024**2,
) -> str

Get the hash of a file

Parameters:

Name Type Description Default
fp Path

Path to the file

required
algorithm str

Algorithm to use during hashing

'sha256'
buffer_size int

The size of the chunks to use when hashing the file

64 * 1024 ** 2

Returns:

Type Description
str

Hash of the file

Source code in src/gcages/hashing.py
def get_file_hash(
    fp: Path, algorithm: str = "sha256", buffer_size: int = 64 * 1024**2
) -> str:
    """
    Get the hash of a file

    Parameters
    ----------
    fp
        Path to the file

    algorithm
        Algorithm to use during hashing

    buffer_size
        The size of the chunks to use when hashing the file

    Returns
    -------
    :
        Hash of the file
    """
    hasher = hashlib.new(algorithm)
    with open(fp, "rb") as fh:
        data = fh.read(buffer_size)
        while data:
            hasher.update(data)
            data = fh.read(buffer_size)

    return hasher.hexdigest()