Shared cryptographic hashing primitives for content integrity verification
hash_bytes
Computes a cryptographic hash of byte content, returning a self-describing string in "algo:hexdigest" format. This format embeds the algorithm name, making hashes forward-compatible if the algorithm changes.
def hash_file( path:Union, # Path to file to hash algo:str='sha256', # Hash algorithm name chunk_size:int=8192, # Read chunk size in bytes)->str: # Hash string in "algo:hexdigest" format
Stream-hash a file without loading it entirely into memory.
import tempfileimport os# Create a temp file with known contentwith tempfile.NamedTemporaryFile(delete=False, mode='wb') as tmp: tmp.write(b"hello world") tmp_path = tmp.name# Hash the filefile_hash = hash_file(tmp_path)print(f"hash_file result: {file_hash}")# Should match hash_bytes of the same contentassert file_hash == hash_bytes(b"hello world")# Test with Path objectassert hash_file(Path(tmp_path)) == file_hash# Cleanupos.unlink(tmp_path)print("hash_file tests passed")