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.
hash_bytes
def hash_bytes( content:bytes, # Byte content to hash algo:str='sha256', # Hash algorithm name (e.g., "sha256", "sha3_256"))->str: # Hash string in "algo:hexdigest" format
SHA-512 result: sha512:ee26b0dd4af7e749aa1a8ee...
Custom algorithm test passed
hash_file
Stream-hashes a file without loading it entirely into memory. Uses chunked reads suitable for large files (audio, video, etc.).
hash_file
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")
Hashes a dict via deterministic JSON canonicalization (sorted keys, no whitespace) before delegating to hash_bytes. Two callers in the substrate:
CR-8compute_config_schema_hash — hashes a plugin’s JSON Schema for drift detection (manifest-stored vs live worker).
CR-7compute_config_hash — hashes a plugin instance’s effective config to key EmpiricalResourceRecord storage by (instance_id, config_hash).
Same canonicalization rules → same stability guarantees across Python versions, dict insertion orders, and machines. None is treated as {} so a missing schema/config still produces a deterministic hash rather than raising.