Adversarial image defense and provenance system written in C.
Hemlock protects images through a dual-layer approach:
-
Perceptual Hash (aHash) - Creates a structural fingerprint by downsampling the image to 8x8 grayscale and comparing each pixel to the average brightness. This detects visual modifications.
-
Data Poisoning + RSA Signing - Adds subtle adversarial noise to the image pixels, then cryptographically signs the result. Any modification (AI training, compression, editing) breaks the signature.
camera.c- Protection tool: generates hash, poisons image, signs itverify.c- Verification tool: checks RSA signature + perceptual hashstb_image.h,stb_image_write.h- Image loading (from stb libraries)private.pem,public.pem- RSA key pairahash.bin- Stored perceptual hashsignature.sig- RSA signature
gcc -o camera camera.c -lssl -lcrypto -lm
gcc -o verify verify.c -lssl -lcrypto -lm# Place your input image as input.jpg
./camera
# Outputs: protected.jpg, ahash.bin, signature.sig./verifyOutput report shows:
- IDENTITY: Whether RSA signature is valid (bit-perfect match)
- CONTENT: Hamming distance between stored and computed perceptual hashes
- 0 bits: Identical
- <5 bits: Authentic (minor compression/noise)
-
5 bits: Tampered (AI or manual edit detected)
Perceptual Hash: Downsample to 8x8, grayscale, compute average, set bits for pixels above average.
Poisoning: Add uniform random noise (5% intensity) to all pixels, making the image useless for AI training while appearing unchanged to humans.
Verification: RSA signature detects bit-level changes; aHash detects visual/content changes.
- Protect artwork from AI scraping and unauthorized training
- Prove image provenance and ownership
- Detect if images have been tampered with or recompressed
- Adversarial defense for photographers and content creators
Two independent checks:
-
RSA Signature (IDENTITY) - Uses SHA-256 with RSA. Any byte-level change to the image pixels breaks the signature. This verifies the file is exactly as you produced it (bit-perfect).
-
Perceptual Hash (CONTENT) - Uses aHash to detect visual changes. Compression artifacts, AI upscaling, or manual edits will change the perceptual structure. The hamming distance indicates how much the visual content differs.
Together they provide:
- Cryptographic proof of authorship (RSA)
- Visual integrity verification (aHash)
- Detection of adversarial modifications (combined)
- Requires original
input.jpgto run protection - Keys are placeholder - generate your own for production
- Perceptual hash is not cryptographic (can be brute-forced)
- Does not prevent metadata stripping
openssl genrsa -out private.pem 2048
openssl rsa -in private.pem -pubout -out public.pem- OpenSSL
- stb_image (included)