Canonical content hashing utilities used by the validation suite.
Part of Foundation
- Format
- PY
- Lines
- 37
- Size
- 1.0 KB
- SHA-256 (v2)
801243a09e128c66163ac8edd0fbb26ba361b66518404eb5147d54b41dcc30d7
from __future__ import annotations import hashlib import json from pathlib import Path from typing import Any def sha256_text(text: str) -> str: """Hash exact UTF-8 text.""" return "sha256:" + hashlib.sha256(text.encode("utf-8")).hexdigest() def canonical_json_bytes(value: Any) -> bytes: """Canonical JSON v1 used by the example package. Object keys are sorted, arrays retain order, insignificant whitespace is removed, Unicode is encoded as UTF-8, and JSON numbers use Python's normal JSON serialization. Production systems that need cross-language numeric canonicalization should adopt a stricter standard and version it. """ return json.dumps( value, ensure_ascii=False, sort_keys=True, separators=(",", ":"), allow_nan=False, ).encode("utf-8") def sha256_json(value: Any) -> str: return "sha256:" + hashlib.sha256(canonical_json_bytes(value)).hexdigest() def load_json(path: Path) -> Any: with path.open("r", encoding="utf-8") as handle: return json.load(handle) |
On this page
No section headings.
Manifest records
documents/raw/foundation/MANIFEST.json
{
"path": "validation/hash_utils.py",
"bytes": 1074,
"sha256": "801243a09e128c66163ac8edd0fbb26ba361b66518404eb5147d54b41dcc30d7"
}site-manifest.json
{
"path": "documents/raw/foundation/validation/hash_utils.py",
"bytes": 1074,
"sha256": "801243a09e128c66163ac8edd0fbb26ba361b66518404eb5147d54b41dcc30d7"
}