Every entry references the one before it.
SHA-256 hash chain where each audit entry contains the hash of the previous entry. Chain starts from a well-known genesis hash. Any modification breaks the chain and is detected immediately. Walk the chain from genesis to verify the entire history.
// Hash chain verification
function verifyChain(entries) {
let prevHash = "genesis";
for (const entry of entries) {
if (entry.prevHash !== prevHash) {
return { valid: false, brokenAt: entry._id };
}
const computed = sha256({
agentId: entry.agentId,
event: entry.event,
details: entry.details,
prevHash: entry.prevHash,
timestamp: entry.timestamp
});
if (computed !== entry.hash) {
return { valid: false, tamperedAt: entry._id };
}
prevHash = entry.hash;
}
return { valid: true, entriesChecked: entries.length };
}