Neural Mastery
← Practice
Verify Audit Log Hash Chain Integrityhard

Verify Audit Log Hash Chain Integrity

hard Agents in Production 15 min

NumPy allowed · plain Python earns 10 bonus points

Implement audit-log hash-chain verification, a real tamper-evidence technique letting a security review detect if any historical agent-action log entry was altered or deleted after the fact.

Task

Implement is_audit_chain_intact(log_entries). Each entry is {"data": str, "prev_hash": str, "hash": str}. For entry 0, prev_hash must be ''. For every entry, its hash must equal hashlib.sha256((entry['prev_hash'] + entry['data']).encode()).hexdigest(). For entry i > 0, its prev_hash must equal entry i-1's hash. Return True only if ALL these checks pass for every entry.

Function Signature

is_audit_chain_intact(log_entries: list[dict]) -> bool

Examples

Example 1: valid two-entry chain
Input: log_entries = [{'data': 'action1', 'prev_hash': '', 'hash': '1d3efef6fc7477ab5f9c0338a187a8dec7ac6198ed1f55119856fca61148abec'}, {'data': 'action2', 'prev_hash': '1d3efef6fc7477ab5f9c0338a187a8dec7ac6198ed1f55119856fca61148abec', 'hash': '35ce289e88d5da601b1447e35a9e36e5f6def8fd35ccbc3728a4e69c6976721e'}]
Output: True
Example 2: single valid entry
Input: log_entries = [{'data': 'only', 'prev_hash': '', 'hash': 'f905b19542ed08c9a9c26543cca32e5711d207dcffb81b4cdb44ce0b989431c9'}]
Output: True
Example 3: tampered data breaks hash
Input: log_entries = [{'data': 'tampered', 'prev_hash': '', 'hash': 'wronghash'}]
Output: False

Constraints

  • log_entries non-empty, in original chronological order
Python3Saved ✓
import hashlib

def is_audit_chain_intact(log_entries):
# Your implementation here
pass

Case 1: valid two-entry chain
Input: log_entries = [{'data': 'action1', 'prev_hash': '', 'hash': '1d3efef6fc7477ab5f9c0338a187a8dec7ac6198ed1f55119856fca61148abec'}, {'data': 'action2', 'prev_hash': '1d3efef6fc7477ab5f9c0338a187a8dec7ac6198ed1f55119856fca61148abec', 'hash': '35ce289e88d5da601b1447e35a9e36e5f6def8fd35ccbc3728a4e69c6976721e'}]
Expected: True
Case 2: single valid entry
Input: log_entries = [{'data': 'only', 'prev_hash': '', 'hash': 'f905b19542ed08c9a9c26543cca32e5711d207dcffb81b4cdb44ce0b989431c9'}]
Expected: True
Case 3: tampered data breaks hash
Input: log_entries = [{'data': 'tampered', 'prev_hash': '', 'hash': 'wronghash'}]
Expected: False