Research-Stack/4-Infrastructure/lib/hashing.py
Devin AI e5f04ee6c3 refactor(infra): extract shared utilities from duplicated code patterns
Create 4-Infrastructure/lib/ with canonical implementations of:
- hashing.py: sha256_bytes, sha256_text, sha256_file
- q16.py: Q16_16 fixed-point constants and arithmetic
- jsonl.py: load_json, load_jsonl, write_jsonl, stable_json, canonical_json_bytes
- fraction_utils.py: Fraction serialization helpers for hardware probes

Refactor 66 files across 4-Infrastructure/{hardware,shim,infra} and
5-Applications/{scripts,tools-scripts,hutter_prize,text-to-cad} to import
from the shared library instead of maintaining local copies.

4-Infrastructure/auto/lib/q16.py now re-exports from lib.q16.

Net: -743 lines (490 added, 1233 removed)

Build: not applicable (Python-only change, py_compile verified on all 71 files)
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
2026-06-15 02:26:45 +00:00

29 lines
796 B
Python

"""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()