19 lines
484 B
Python
19 lines
484 B
Python
"""RFC 8785 canonical JSON hashing shared by persisted records."""
|
|
|
|
from hashlib import sha256
|
|
|
|
import rfc8785
|
|
|
|
from app.domain.domain_errors import DomainError
|
|
|
|
|
|
def canonical(value: object) -> bytes:
|
|
try:
|
|
return rfc8785.dumps(value)
|
|
except rfc8785.CanonicalizationError as exc:
|
|
raise DomainError("SCHEMA_INVALID", "Content is not canonicalizable JSON") from exc
|
|
|
|
|
|
def digest(value: object) -> str:
|
|
return "sha256:" + sha256(canonical(value)).hexdigest()
|