Skip to content

check_sha1

check_sha1(filename: str, sha1_hash: str) -> bool

Check whether the sha1 hash of the file content matches the expected hash.

Code borrowed from dgl

Parameters:

Name Type Description Default
filename str

Path to the file.

required
sha1_hash str

Expected sha1 hash in hexadecimal digits.

required

Returns:

Type Description
bool

Whether the file content matches the expected hash.

Source code in mlx_graphs/datasets/utils/download.py
def check_sha1(filename: str, sha1_hash: str) -> bool:
    """Check whether the sha1 hash of the file content matches the expected hash.

    Code borrowed from dgl

    Args:
        filename: Path to the file.
        sha1_hash: Expected sha1 hash in hexadecimal digits.

    Returns:
        Whether the file content matches the expected hash.
    """
    # SHA1 used for download integrity check, not security
    sha1 = hashlib.sha1(usedforsecurity=False)
    with open(filename, "rb") as f:
        while True:
            data = f.read(1048576)
            if not data:
                break
            sha1.update(data)

    return sha1.hexdigest() == sha1_hash

from mlx_graphs.datasets.utils import check_sha1 · mlx_graphs/datasets/utils/download.py