Developer guide

How to Verify AI Output Integrity with SHA-256 and Cryptographic Receipts

A SHA-256 digest is a fingerprint of data. If even one character of an AI output changes, its digest changes. This guide shows how to produce a stable digest for any JSON payload, and how EFUPAL proof receipts use one so anyone can check that a result was not altered.

1. Serialize JSON deterministically

Two systems can write the same object with keys in a different order, which gives different digests. Sorting keys first makes the text, and so the digest, identical every time.

canonical-json.ts
// Deterministic JSON: object keys sorted, arrays kept in order.
export function canonicalJson(value: unknown): string {
  if (Array.isArray(value)) return `[${value.map(canonicalJson).join(",")}]`;
  if (value && typeof value === "object") {
    const obj = value as Record<string, unknown>;
    return `{${Object.keys(obj).sort()
      .map((k) => `${JSON.stringify(k)}:${canonicalJson(obj[k])}`)
      .join(",")}}`;
  }
  return JSON.stringify(value ?? null);
}

2. Generate a SHA-256 digest with Web Crypto

Web Crypto is built into browsers and modern server runtimes, so there is nothing to install.

sha256.ts
// Web Crypto SHA-256 — works in browsers, Node 18+, Deno, Bun and edge runtimes.
export async function sha256Hex(text: string): Promise<string> {
  const bytes = new TextEncoder().encode(text);
  const digest = await crypto.subtle.digest("SHA-256", bytes);
  return Array.from(new Uint8Array(digest), (b) => b.toString(16).padStart(2, "0")).join("");
}

// Fingerprint any payload so the same data always yields the same digest.
export const payloadDigest = (payload: unknown) => sha256Hex(canonicalJson(payload));

3. Compute an EFUPAL proof digest

EFUPAL-UPP-v1 receipts store the digest in payload_sha256. The checker hashes seven body fields in a fixed order, so build the body the same way.

upp-digest.ts
// EFUPAL-UPP-v1: the checker hashes these seven fields, in exactly this order.
const BODY_FIELDS = ["protocol","kem_algorithm","sig_algorithm","issuer","subject_domain","claim","timestamp_utc"] as const;

export async function uppDigest(proof: Record<string, string>): Promise<string> {
  const body = Object.fromEntries(BODY_FIELDS.map((f) => [f, proof[f]]));
  return sha256Hex(JSON.stringify(body));
}

4. Try a sample proof

Copy this sample and paste it into the checker. It reports a matching digest. Change any letter in claim and it reports a mismatch.

sample-proof.json
{
  "protocol": "EFUPAL-UPP-v1",
  "kem_algorithm": "ML-KEM-768",
  "sig_algorithm": "ML-DSA-65",
  "issuer": "did:web:efupal.ai",
  "subject_domain": "efupal.ai",
  "claim": "model-output:sha256-guide-example",
  "timestamp_utc": "2026-11-22T00:00:00Z",
  "payload_sha256": "250b9553693584c48318f58d0f5ab7f6e248024001b399004c60a53e6964bd84",
  "signature_b64": "c2FtcGxlLW5vdC1zaWduZWQ="
}

A matching digest proves integrity only. It does not prove who signed the receipt: the sample signature is a placeholder and the checker does not verify signatures.