Metadata-Version: 2.4
Name: ledger-verify
Version: 0.1.0
Summary: Tamper-evident hash-chain audit trail verifier: a tiny hash-chained append-only log, a verifier that names the first broken entry, and a crypto-shredding demo.
Author-email: Aben Consulting <info@aben.ch>
License-Expression: Apache-2.0
Project-URL: Homepage, https://aben.ch/notes/tamper-evident-audit-trails/
Project-URL: Source, https://github.com/aben-consulting/ledger-verify
Project-URL: Issues, https://github.com/aben-consulting/ledger-verify/issues
Keywords: audit-log,hash-chain,tamper-evident,gdpr,crypto-shredding
Classifier: Development Status :: 4 - Beta
Classifier: Environment :: Console
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Security
Classifier: Topic :: System :: Logging
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Provides-Extra: shred
Requires-Dist: cryptography>=41; extra == "shred"
Provides-Extra: test
Requires-Dist: pytest; extra == "test"
Requires-Dist: cryptography>=41; extra == "test"
Dynamic: license-file

# ledger-verify

A tiny, dependency-free **hash-chained append-only log**, a **verifier that names the first broken entry**, and a **crypto-shredding demo** for GDPR-style erasure. Under 200 lines of library code.

It is a teaching/reference implementation of a general pattern, not a production audit system.

```text
$ ledger-verify demo
intact: True
tampered: first broken entry = 2 (entry content does not match its hash)
tail removed, no anchor  : verifies -> True (undetected without an anchored head)
tail removed, with anchor: verifies -> False (head differs from the anchored head (tail removed or replaced?))
```

## Install and run

```sh
git clone https://github.com/aben-consulting/ledger-verify.git && cd ledger-verify
pip install -e ".[shred]"      # [shred] adds `cryptography` for the erasure demo
ledger-verify demo
python examples/audit_demo.py
pytest -q                      # pip install -e ".[test]" first
```
Python 3.9+. The core (`ledgerverify.chain`) has no dependencies.

## How it works

Every entry stores `hash = SHA-256(prev_hash | seq | canonical_json(payload))`. Change, reorder or delete an entry in the middle and every later hash stops matching. `verify()` reports the **first** entry that fails and why.

```python
from ledgerverify import Ledger
l = Ledger()
l.append({"event": "finding.created", "id": "F-1"})
anchor = l.head                 # store this OUTSIDE the log (another system, signed checkpoint…)
l.append({"event": "finding.approved", "id": "F-1"})
print(l.verify(expected_head=l.head).ok)   # later: l.verify(expected_head=anchor)
```

CLI on a JSON Lines file (one entry per line, as written by `Ledger.dump_jsonl`):
```sh
ledger-verify verify ledger.jsonl [--head <hash stored elsewhere>]
```

### Crypto-shredding (`ledgerverify.shred`)
Seal personal fields with a per-subject AES-256-GCM key and put only the ciphertext in the log. To erase a person, destroy their key: the log is unchanged, still verifies, and the data is unreadable.

## Threat model — read this

| Attack | Detected? |
|---|---|
| Edit an entry's content | Yes — names the entry |
| Delete or reorder an entry in the middle | Yes |
| Delete the **tail** of the log | **Only** if you pass an externally stored head (`expected_head`) |
| Rewrite the whole chain consistently | **No** — anchor the head somewhere the attacker can't write |
| Record a false event in the first place | No — the chain proves immutability, not truth |

Not included: persistence beyond JSONL, concurrency/locking, signatures, external timestamping, key wrapping under a KMS/HSM, key backup, rotation. `KeyStore` keeps keys in memory. Data derived from the plaintext (indexes, logs, backups) is outside the protection. Whether crypto-shredding satisfies erasure obligations for you is a question for your legal and compliance advisers.

## Contributing & security
Issues and pull requests are welcome. If you find a flaw in the threat model above, please open an issue — that is the most useful feedback.

## Licence
Apache License 2.0 — see [LICENSE](LICENSE). © Áben Consulting Kft., Budapest. https://aben.ch
