keyanchor 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
keyanchor/__init__.py ADDED
@@ -0,0 +1,3 @@
1
+ """keyanchor: bind classical SSH identity keys to post-quantum keys and anchor the binding in time."""
2
+
3
+ __version__ = "0.1.0"
keyanchor/__main__.py ADDED
@@ -0,0 +1,5 @@
1
+ import sys
2
+
3
+ from .cli import main
4
+
5
+ sys.exit(main())
keyanchor/canon.py ADDED
@@ -0,0 +1,62 @@
1
+ """Canonical statement bytes and content addresses.
2
+
3
+ Statements use only strings, integers, objects and arrays. For that subset, sorted keys and compact
4
+ separators give the RFC 8785 (JCS) form; floats, booleans and null are rejected so the subset holds.
5
+ """
6
+ from __future__ import annotations
7
+
8
+ import hashlib
9
+ import json
10
+
11
+ MAX_INT = 2**53 - 1
12
+
13
+
14
+ class CanonError(ValueError):
15
+ pass
16
+
17
+
18
+ def _check(value: object, depth: int = 0) -> None:
19
+ if depth > 8:
20
+ raise CanonError("statement nested too deeply")
21
+ if isinstance(value, bool) or value is None or isinstance(value, float):
22
+ raise CanonError(f"unsupported JSON value: {value!r}")
23
+ if isinstance(value, int):
24
+ if abs(value) > MAX_INT:
25
+ raise CanonError("integer outside ±(2^53-1)")
26
+ elif isinstance(value, str):
27
+ pass
28
+ elif isinstance(value, list):
29
+ for item in value:
30
+ _check(item, depth + 1)
31
+ elif isinstance(value, dict):
32
+ for key, item in value.items():
33
+ if not isinstance(key, str) or not key.isascii():
34
+ raise CanonError(f"member names must be ASCII strings: {key!r}")
35
+ _check(item, depth + 1)
36
+ else:
37
+ raise CanonError(f"unsupported type: {type(value).__name__}")
38
+
39
+
40
+ def canonical(statement: dict) -> bytes:
41
+ if not isinstance(statement, dict):
42
+ raise CanonError("statement must be a JSON object")
43
+ _check(statement)
44
+ try:
45
+ return json.dumps(statement, sort_keys=True, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
46
+ except UnicodeEncodeError as exc: # lone surrogate from a "\ud800" escape
47
+ raise CanonError("string is not valid Unicode") from exc
48
+
49
+
50
+ def content_address(raw: bytes) -> str:
51
+ return hashlib.sha256(raw).hexdigest()
52
+
53
+
54
+ def parse_canonical(raw: bytes) -> dict:
55
+ """Parse statement bytes; reject anything that is not byte-identical to its canonical form."""
56
+ try:
57
+ statement = json.loads(raw.decode("utf-8"))
58
+ except (UnicodeDecodeError, json.JSONDecodeError) as exc:
59
+ raise CanonError(f"not JSON: {exc}") from exc
60
+ if canonical(statement) != raw:
61
+ raise CanonError("statement bytes are not canonical")
62
+ return statement