Research / Engineering Blog / Evidence
Hashing a JSON record so that someone else can check it
A digest is only worth something if another party can reproduce it. What we had to pin down before a SHA-256 over a JSON record meant anything.
Every confirmed classification is stored as a record with a seal: a SHA-256 digest over the record. The point of the seal is that an auditor can recompute it without us. That sounds like one line of code. It took rather more, and almost none of it was cryptography.
Same content, different bytes
These two documents say the same thing:
{ "unit": "nm", "value": 1550 }
{ "value": 1550, "unit": "nm" }Their digests have nothing in common. A hash is taken over bytes, and JSON does not fix the bytes: key order, whitespace and the handling of missing members are all left to the writer. A database that stores JSON in a binary form may hand the keys back in a different order than it received them. If the digest depends on that order, a record can fail verification without anyone having touched it.
A canonical form
So the digest is never taken over the stored bytes. It is taken over a canonical form that any reader can rebuild from the content:
function canonical(value: unknown): unknown {
if (value === null || typeof value !== "object") return value ?? null;
if (Array.isArray(value)) return value.map(canonical);
const out: Record<string, unknown> = {};
for (const key of Object.keys(value).sort()) {
const member = (value as Record<string, unknown>)[key];
if (key === "hash" || member === undefined) continue;
out[key] = canonical(member);
}
return out;
}
const seal = (record: object) =>
createHash("sha256").update(JSON.stringify(canonical(record))).digest("hex");The rules are short on purpose:
- members of an object are ordered by key, at every level;
- a member without a value is left out, a null stays a null;
- lists keep their order, because order in a list means something;
- the member that holds the digest is left out;
- no insignificant whitespace.
Three things we learned
Never recompute an old digest. When the format gains a field, the tempting migration is to rewrite every stored record and seal it again. That would turn the seal into a statement about today instead of the day of confirmation. A record written under an earlier version is checked by the rules of that version, and its digest is left alone.
New fields must be invisible when absent. Because a missing member is left out of the canonical form, a record that does not carry a newer optional block hashes exactly as it did before the block existed. Every addition to the format is checked against that rule before it ships.
Seal what is shown. For search, a few values of a record also live in columns beside it. Columns outside the digest can be changed without breaking it. So the record carries a sealed block that repeats them, with a version number, and verification compares the columns against that block.
What a seal does not say
A matching digest shows that a record is unchanged. It does not show that a fact in it was right. And a hash chain over the audit log shows order and completeness only as long as its head is held somewhere the writer of the log cannot reach. The full treatment, including the bound on undetected change, is in A Verifiable Evidence Record; the rules above are specified in the Open Format.
