File size: 761 Bytes
d42cde7 10846ce d42cde7 10846ce d42cde7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import json
import hashlib
from datetime import datetime, timezone
from typing import Any, Dict
DRP_EVENT_SPEC = "drp-event-v0"
DRP_BUNDLE_SPEC = "drp-bundle-v0"
def canon(obj: Any) -> bytes:
"""Canonical JSON encoding (stable for hashing/verifying across machines)."""
return json.dumps(
obj,
ensure_ascii=False,
sort_keys=True,
separators=(",", ":"),
).encode("utf-8")
def sha256_hex(b: bytes) -> str:
return hashlib.sha256(b).hexdigest()
def now_utc_iso() -> str:
return datetime.now(timezone.utc).isoformat()
def hash_event(event: Dict[str, Any]) -> str:
"""Hash event excluding its own 'hash' field (if present)."""
e = dict(event)
e.pop("hash", None)
return sha256_hex(canon(e)) |