examples/detect_tampered_output.py
| 1 | """Detect tampered AI output. |
| 2 | |
| 3 | Run: python examples/detect_tampered_output.py |
| 4 | """ |
| 5 | |
| 6 | from __future__ import annotations |
| 7 | |
| 8 | from quantumshield import AgentIdentity |
| 9 | |
| 10 | from pqc_content_provenance import ( |
| 11 | ContentManifest, |
| 12 | GenerationContext, |
| 13 | ManifestSigner, |
| 14 | ModelAttribution, |
| 15 | ) |
| 16 | |
| 17 | |
| 18 | def main() -> None: |
| 19 | identity = AgentIdentity.create("signer") |
| 20 | signer = ManifestSigner(identity) |
| 21 | |
| 22 | original = b"The patient has low risk of myocardial infarction." |
| 23 | manifest = ContentManifest.create( |
| 24 | content=original, |
| 25 | content_type="text/plain", |
| 26 | model_attribution=ModelAttribution( |
| 27 | model_did="did:pqaid:medical-ai", |
| 28 | model_name="Medical-Diagnostics-v2", |
| 29 | model_version="2.3", |
| 30 | ), |
| 31 | generation_context=GenerationContext( |
| 32 | prompt_hash="c" * 64, |
| 33 | parameters={"temperature": 0.1}, |
| 34 | generated_at="2026-04-20T12:00:00Z", |
| 35 | ), |
| 36 | ) |
| 37 | signed = signer.sign(manifest) |
| 38 | |
| 39 | # Attacker modifies output (swaps "low" for "high") |
| 40 | tampered = b"The patient has high risk of myocardial infarction." |
| 41 | |
| 42 | result = ManifestSigner.verify(signed, tampered) |
| 43 | if result.valid: |
| 44 | print("[FAIL] Tampering was not detected!") |
| 45 | else: |
| 46 | print("[OK] Tampering detected:") |
| 47 | print(f" content_hash_match: {result.content_hash_match}") |
| 48 | print(f" signature_match: {result.signature_match}") |
| 49 | print(f" error: {result.error}") |
| 50 | |
| 51 | |
| 52 | if __name__ == "__main__": |
| 53 | main() |
| 54 | |