numos/canonical.py
back to source
"""Canonical serialization and content hashing for numos.state."""
import hashlib
def canonical_body(lines):
"""Join state lines into the exact bytes covered by the C record.
The C line is excluded so a state file can be hashed before and after
the hash is stamped into it and get the same answer both times.
"""
body = [ln for ln in lines if not ln.startswith("C ")]
return "".join(ln + "\n" for ln in body)
def content_hash(lines):
"""Return the lowercase hex sha256 of the canonical body."""
return hashlib.sha256(canonical_body(lines).encode("ascii")).hexdigest()