Source code for askemblaex.hash

"""
askemblaex/hash.py

Content-based file hashing utilities.
"""

from __future__ import annotations

import hashlib
from pathlib import Path

BUF_SIZE = 1024 * 1024  # 1MB chunks


[docs] def hash_file(path: Path, algo: str = "sha256") -> str: """ Hash a file by content and return the hex digest. Reads the file in chunks so large files do not require loading the entire content into memory. Args: path: Path to the file to hash. algo: Hash algorithm name accepted by hashlib (default "sha256"). Returns: Lowercase hex digest string e.g. "2e7698fc5e3744fd...". Raises: FileNotFoundError: If path does not exist. """ h = hashlib.new(algo) with path.open("rb") as f: for chunk in iter(lambda: f.read(BUF_SIZE), b""): h.update(chunk) return h.hexdigest()