RFTSystems commited on
Commit
d42cde7
·
verified ·
1 Parent(s): 5dacd43

Create drp/canon.py

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