Coverage for src/governed_harness/evidence/hashing.py: 100%
17 statements
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
« prev ^ index » next coverage.py v7.17.2, created at 2026-11-12 16:06 +0000
1from __future__ import annotations
3import hashlib
4import json
5from pathlib import Path
6from typing import Any
9def sha256_bytes(data: bytes) -> str:
10 return f"sha256:{hashlib.sha256(data).hexdigest()}"
13def canonical_json_bytes(value: Any) -> bytes:
14 return json.dumps(
15 value,
16 sort_keys=True,
17 separators=(",", ":"),
18 ensure_ascii=False,
19 default=str,
20 ).encode("utf-8")
23def sha256_json(value: Any) -> str:
24 return sha256_bytes(canonical_json_bytes(value))
27def sha256_file(path: Path, chunk_size: int = 1024 * 1024) -> str:
28 digest = hashlib.sha256()
29 with path.open("rb") as handle:
30 while chunk := handle.read(chunk_size):
31 digest.update(chunk)
32 return f"sha256:{digest.hexdigest()}"