Verify that an audit log was not edited
An audit log is only evidence if you can tell whether someone changed it. A hash chain is the simplest structure that lets you tell. This article builds one, runs a verifier on it, breaks it four different ways, and shows which breaks the chain catches by itself and which need one more thing from you.
Everything below runs with Python's standard library on macOS or Linux. The sample log is deterministic, so you will get exactly the hashes shown.
What is a tamper-evident log, and is it tamper-proof?
A tamper-evident log lets a verifier detect a change after the fact. It does not stop anyone with write access from making the change. Tamper-proof storage would prevent the change, and no log on a computer someone else controls is that. The goal is narrower and achievable: any edit, deletion or reordering of events you have already seen is detected.
NIST's log management guide, SP 800-92, describes the basic form for log files: "Log file integrity checking involves calculating a message digest for each file and storing the message digest securely to ensure that changes to archived logs are detected." A hash chain extends that from whole files to individual events.
How does a hash chain link events?
Each event includes the hash of the event before it. Here the hash is SHA-256 over the previous line's exact bytes, and the first event points at 64 zeros:
event 1: prev = 0000…0000
event 2: prev = SHA-256(event 1 as written)
event 3: prev = SHA-256(event 2 as written)
Change any byte of event 2 and its hash changes, so event 3's prev no longer matches. To hide the edit, an attacker
must recompute every later event's prev, and the last hash of the log changes. That last hash is a fingerprint of
the entire history up to that point.
Hash the bytes as written, not a re-encoded object. Two JSON encoders can write the same object with different spacing or key order, and then the hashes differ for no reason. If you must hash a parsed object, use a canonical form such as RFC 8785 (JSON Canonicalization Scheme).
Build a sample log
make-log.py writes four events as JSON Lines, one event per line, each chained to the one before:
import hashlib, json
events = [
{"time": "2026-12-01T09:00:00Z", "event": "create", "secret": "stripe-live", "who": "ana"},
{"time": "2026-12-01T09:05:12Z", "event": "grant", "secret": "stripe-live", "who": "ana", "to": "billing-agent"},
{"time": "2026-12-02T03:14:07Z", "event": "read", "secret": "stripe-live", "who": "billing-agent"},
{"time": "2026-12-03T03:14:09Z", "event": "read", "secret": "stripe-live", "who": "billing-agent"},
]
prev = "0" * 64
with open("audit.jsonl", "w", newline="\n") as f:
for e in events:
line = json.dumps({"prev": prev, **e}, separators=(",", ":"))
f.write(line + "\n")
prev = hashlib.sha256(line.encode()).hexdigest()
$ python3 make-log.py && cat audit.jsonl
{"prev":"0000000000000000000000000000000000000000000000000000000000000000","time":"2026-12-01T09:00:00Z","event":"create","secret":"stripe-live","who":"ana"}
{"prev":"03d09b9046826be0eebd814bfa3cc953211e39ed3d3996bed67d3302c65b1c70","time":"2026-12-01T09:05:12Z","event":"grant","secret":"stripe-live","who":"ana","to":"billing-agent"}
{"prev":"856fe583fc77a9897a03b4d09ef6c2d0f901c5cd75e231c9dd5b9e027d1ae6ed","time":"2026-12-02T03:14:07Z","event":"read","secret":"stripe-live","who":"billing-agent"}
{"prev":"d2ff4e237c1aacca497a04d5359d79d0eaf0c2ebb54e50f9397617cf208f7fca","time":"2026-12-03T03:14:09Z","event":"read","secret":"stripe-live","who":"billing-agent"}
The verifier
verify-chain.py walks the log, checks each prev, and prints the last hash. With --expect it also checks that the
log ends at a hash you kept; with --after it checks a batch of new events that continues from one:
#!/usr/bin/env python3
"""Verify a hash-chained JSON Lines log.
Each line holds "prev": the SHA-256 (hex) of the previous line's exact
bytes, without its newline. The first line's "prev" is 64 zeros, or,
with --after, the last hash you kept from an earlier check.
"""
import argparse
import hashlib
import json
import sys
parser = argparse.ArgumentParser()
parser.add_argument("log")
parser.add_argument("--after", default="0" * 64,
help="hash the first line must chain onto")
parser.add_argument("--expect", help="hash the log must end at")
args = parser.parse_args()
prev = args.after.lower()
count = 0
with open(args.log, "rb") as f:
for number, raw in enumerate(f, start=1):
line = raw.rstrip(b"\n")
if not line:
continue
try:
claimed = json.loads(line)["prev"]
if not isinstance(claimed, str):
raise TypeError
except (ValueError, KeyError, TypeError):
sys.exit(f"BROKEN at line {number}: not a chained event")
if claimed != prev:
sys.exit(f"BROKEN at line {number}: prev is {claimed[:12]}..., "
f"expected {prev[:12]}...")
prev = hashlib.sha256(line).hexdigest()
count += 1
if args.expect and args.expect.lower() != prev:
sys.exit(f"MISMATCH: you kept {args.expect.lower()}\n"
f" log ends at {prev}")
print(f"OK: {count} events, last hash {prev}")
On the untouched log:
$ python3 verify-chain.py audit.jsonl
OK: 4 events, last hash 79c1189d3f3009ae371bc8bfc4fb73408590ab59fc4584a5dab779625dd41d98
Write that last hash down somewhere else. The rest of this article is about why.
Which kinds of tampering does a hash chain catch?
1. Edit an event. Pretend the billing agent's first read was really Ana's:
$ sed '3s/"who":"billing-agent"/"who":"ana"/' audit.jsonl > edited.jsonl
$ python3 verify-chain.py edited.jsonl
BROKEN at line 4: prev is d2ff4e237c1a..., expected 729032da0380...
Caught. The verifier names line 4, the first line whose prev no longer matches, so line 3 was changed (or line 4's
own prev was).
2. Delete an event from the middle.
$ sed '3d' audit.jsonl > removed.jsonl
$ python3 verify-chain.py removed.jsonl
BROKEN at line 3: prev is d2ff4e237c1a..., expected 856fe583fc77...
Caught.
3. Cut off the newest events.
$ sed '$d' audit.jsonl > truncated.jsonl
$ python3 verify-chain.py truncated.jsonl
OK: 3 events, last hash d2ff4e237c1aacca497a04d5359d79d0eaf0c2ebb54e50f9397617cf208f7fca
Not caught by the chain alone. What is left is a perfectly valid, shorter chain. The same goes for an edit to the newest event: no later event carries its hash.
4. Edit an event and rewrite every hash after it. Anyone who can write the log can run the same code that built
it. rechain.py recomputes every prev in order:
import hashlib, json, sys
prev = "0" * 64
for raw in open(sys.argv[1], "rb"):
event = json.loads(raw)
event["prev"] = prev
line = json.dumps(event, separators=(",", ":"))
print(line)
prev = hashlib.sha256(line.encode()).hexdigest()
$ python3 rechain.py edited.jsonl > forged.jsonl
$ python3 verify-chain.py forged.jsonl
OK: 4 events, last hash 88249d27716c5d86e2f21d0cc77f5773bdfd744b29bc442f3d392dfc893139b4
Not caught by the chain alone either. The forged log is internally consistent.
Now compare both against the hash you kept:
$ python3 verify-chain.py truncated.jsonl --expect 79c1189d3f3009ae371bc8bfc4fb73408590ab59fc4584a5dab779625dd41d98
MISMATCH: you kept 79c1189d3f3009ae371bc8bfc4fb73408590ab59fc4584a5dab779625dd41d98
log ends at d2ff4e237c1aacca497a04d5359d79d0eaf0c2ebb54e50f9397617cf208f7fca
$ python3 verify-chain.py forged.jsonl --expect 79c1189d3f3009ae371bc8bfc4fb73408590ab59fc4584a5dab779625dd41d98
MISMATCH: you kept 79c1189d3f3009ae371bc8bfc4fb73408590ab59fc4584a5dab779625dd41d98
log ends at 88249d27716c5d86e2f21d0cc77f5773bdfd744b29bc442f3d392dfc893139b4
Both caught. This is the part most explanations of hash chains skip: a hash chain protects history only up to a hash someone kept outside the log's control. Without that anchor, the operator of the log, or an attacker who has taken it over, can present any history that is internally consistent.
How do I check new events without re-reading the whole log?
Keep the last hash from each check, and verify only the new events against it with --after. Here two new events
arrive after the four above, saved as new.jsonl:
{"prev":"79c1189d3f3009ae371bc8bfc4fb73408590ab59fc4584a5dab779625dd41d98","time":"2026-12-04T03:14:05Z","event":"read","secret":"stripe-live","who":"billing-agent"}
{"prev":"7638adc4b0402f438fe2e6c4faa502682c42a680bad5b5ee4725ecf7c1d97cf2","time":"2026-12-04T11:02:41Z","event":"read","secret":"stripe-live","who":"laptop-2"}
$ python3 verify-chain.py new.jsonl --after 79c1189d3f3009ae371bc8bfc4fb73408590ab59fc4584a5dab779625dd41d98
OK: 2 events, last hash 2b9beeac15fd3a017e8f0b0856ca78e9a85e600571033e47e1904066def737c3
If someone rewrote the chain, the new batch would not chain onto your kept hash, and the first line would be reported
as broken. An edit that left later hashes alone is not visible in the new batch; it is caught when the full log is
checked against your kept hash. So keep your own copy of the events you verified, or re-check the full export now
and then. A log with limited retention will not start at 64 zeros; start such a check with --after set to the first
remaining event's prev, and confirm that it is a hash you kept.
A small scheduled job, or an AI agent with a schedule, can do this every night:
- Fetch the events since the last run.
- Verify them with
--afterthe hash kept from the last run. - Keep the new last hash somewhere the log's operator cannot change: your own repository, a ticket, an email to yourself, a different cloud account.
- Report anything unexpected: a broken chain, an unfamiliar reader, a secret no one has read in 90 days.
What does a hash chain not prove?
- That every event was logged. A chain shows that recorded events were not changed afterwards. It cannot show that an action was recorded at all. A system that skips writing an event leaves a perfectly valid chain.
- That timestamps are true. The time in an event is whatever the writer put there.
- That everyone sees the same log. A dishonest operator could show you one history and your auditor another. Each chain verifies. Comparing kept hashes at the same position, or checking that one person's later events chain from the other's kept hash, detects it. Published research on tamper-evident logging uses "a mechanism, such as a gossip protocol, to distribute the signed commitments" between clients and auditors (Crosby and Wallach, USENIX Security 2009).
- Who wrote it. Anyone can build a valid chain. A hash the log's operator signs proves which history the operator presented, so a fork can be pinned on them.
- Anything before the anchor. Events written before you first kept a hash are only as trustworthy as whoever held the log until then.
When do I need a Merkle tree instead of a chain?
A chain is enough when you verify every event anyway, as the nightly job above does. It gets expensive when you want to prove one event is in a very large log, because the proof is every event from that one to the hash you trust. Crosby and Wallach put it this way: "Where a classic hash chain might require an 800 MB trace to prove that a randomly chosen event is in a log with 80 million events, our prototype returns a 3 KB proof…"
Merkle trees fix that. Certificate Transparency logs use them for two short proofs: that an entry is in the log (an inclusion proof) and that today's log extends yesterday's (a consistency proof, which, in the RFC's words, proves "the append-only property of the tree"). RFC 6962 has since been obsoleted by RFC 9162, which keeps the same tree.
A related idea protects the log's writer against a later break-in: Schneier and Kelsey's "Secure audit logs to support computer forensics" (1999) evolves the logging key after every entry, so an attacker who takes over the machine cannot read or undetectably alter entries written before the compromise. Like a hash chain, it still relies on values sent off the machine to catch a log whose tail has been cut off.
How is SecureSecretStore's audit trail designed?
SecureSecretStore is not open yet. In its design, every event (create, read, grant, revoke, destroy, sign-in and key change) records the principal, the API key, the time, the source IP and the client, and carries the hash of the event before it. The trail can be exported as JSON and CSV, read through the API since a point you choose, and sent to you by webhook as events happen, which gives you a place outside the service to keep the last hash. The exact export format will be published in the docs at launch; the verifier above shows the general method.
For the other half of what an encrypted store can and cannot prove, see What an end-to-end encrypted secret store can and cannot see. For an agent that runs this check on a schedule without ever handling a secret value, see How to give an AI agent an API key without putting it in the prompt.
For agents and tools: llms.txt. AI version of this article.