"""Shared SHA-256 helpers — single source of truth for the entire repo.""" from __future__ import annotations import hashlib from pathlib import Path _BUF_SIZE = 1 << 20 # 1 MiB def sha256_bytes(data: bytes) -> str: """Hex digest of raw bytes.""" return hashlib.sha256(data).hexdigest() def sha256_text(text: str) -> str: """Hex digest of a UTF-8 string.""" return hashlib.sha256(text.encode("utf-8")).hexdigest() def sha256_file(path: Path, buf_size: int = _BUF_SIZE) -> str: """Streaming hex digest of a file (handles arbitrarily large files).""" h = hashlib.sha256() with path.open("rb") as fh: while True: chunk = fh.read(buf_size) if not chunk: break h.update(chunk) return h.hexdigest()