Skip to main content

🛡️ ThreatEngine

Crates.io Rust Edition Python Bindings License Author

ThreatEngine is a high-performance, 100% native Rust implementation of all 4 Meta (Facebook) ThreatExchange core components: PDQ (Image), TMK (Video), vPDQ (Video PDQ), and HMA (Hasher-Matcher-Actioner) with Python bindings (PyO3).


✨ Full Component Coverage

  • 📷 PDQ (Photo Hashing): 256-bit perceptual image signatures via 2D Discrete Cosine Transform (DCT) & Median Quantization.
  • 🎬 TMK+PDQF (TMK Video Hashing): Aggregates frame-level PDQF signatures across video timelines using Fourier/Cosine basis functions $\psi_m(t)$.
  • 📽️ vPDQ (Video PDQ): Frame-overlap video similarity algorithm for sub-clip and video segment matching based on shared similar frames.
  • 🛡️ HMA (Hasher-Matcher-Actioner Engine): In-memory high-speed indexing, lookup, and action trigger platform (BLOCK, REVIEW, ALLOW) for Trust & Safety workflows.
  • ⚡ Zero-Cost Performance: Native SIMD and bitwise Hamming distance calculations in Rust (10x-50x faster than pure Python loops).

🚀 Installation

Rust (Cargo Crate)

Add threatengine to your Cargo.toml:

[dependencies]
threatengine = "0.1.0"

Or run:

cargo add threatengine

Python Package (PyO3)

pip install threatengine

📖 Usage Examples

🦀 Rust Example

use threatengine::{
    generate_pdq_hash, pdq_similarity, hash_video_file,
    compute_vpdq_similarity, HmaEngine, MatchAction,
};
use image::open;

fn main() {
    // 1. PDQ Image Hashing
    let img1 = open("photo1.jpg").unwrap();
    let img2 = open("photo2.jpg").unwrap();

    let (hash1, q1) = generate_pdq_hash(&img1);
    let (hash2, q2) = generate_pdq_hash(&img2);

    let (distance, similarity) = pdq_similarity(&hash1, &hash2);
    println!("Hamming Distance: {} bits ({:.2}% similarity)", distance, similarity * 100.0);

    // 2. TMK Video Hashing
    let sig1 = hash_video_file("video1.mp4", 2.0).unwrap();
    let sig2 = hash_video_file("video2.mp4", 2.0).unwrap();
    println!("TMK Score: {:.4}", sig1.match_score(&sig2));

    // 3. HMA Engine Indexing & Action Triggering
    let mut hma = HmaEngine::new();
    hma.add_record("media_001", "known_bad_image", hash1, MatchAction::Block);

    let matches = hma.match_hash(&hash1, 31);
    println!("HMA Matches: {}, Action: {:?}", matches.len(), matches[0].action);
}

🐍 Python Example

import threatengine

# 1. Image Hashing & Distance
hash1, quality1 = threatengine.pdq_hash_file("photo1.jpg")
hash2, quality2 = threatengine.pdq_hash_file("photo2.jpg")

distance, similarity = threatengine.pdq_similarity(hash1, hash2)
print(f"Hamming Distance: {distance} bits (Match: {distance <= 31})")

# 2. TMK Video Comparison
score = threatengine.tmk_compare_videos("video1.mp4", "video2.mp4", fps=2.0)
print(f"TMK Video Match Score: {score:.4f}")

# 3. vPDQ Frame-Overlap Comparison
vpdq_score = threatengine.vpdq_match_hashes([hash1], [hash2], threshold=31)
print(f"vPDQ Similarity Score: {vpdq_score:.4f}")

👨‍💻 Author

Crafted with ❤️ by Shakib Ahmed (@expertskb)

📜 License

Distributed under the MIT License. See LICENSE for more details.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

threatengine-0.3.0.tar.gz (18.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

threatengine-0.3.0-cp38-abi3-manylinux_2_34_x86_64.whl (1.2 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.34+ x86-64

File details

Details for the file threatengine-0.3.0.tar.gz.

File metadata

  • Download URL: threatengine-0.3.0.tar.gz
  • Upload date:
  • Size: 18.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for threatengine-0.3.0.tar.gz
Algorithm Hash digest
SHA256 4050e18dda9cafad16f3f8bca611bda2ea6dcf0a44fd83667689ddcc948c3e56
MD5 1d748d72df5b8cccc268e1f4980cdc83
BLAKE2b-256 5301f4229c7e0e056b40511fbb1fb63c815fbaf9330af82f6f9a0580c81358ea

See more details on using hashes here.

File details

Details for the file threatengine-0.3.0-cp38-abi3-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for threatengine-0.3.0-cp38-abi3-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 097d5c40563a9cb4e3ab1dfae48f1efd3476f5e6f725e28c93fcf73477473b8a
MD5 bab8375d3d99fa8c52aad08bf26103db
BLAKE2b-256 84afa5589403452a9f157c58a9468d1da98bb22074ab60f7200609320f7310d8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.3.0 This release

2 files

0.2.0

1 file

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page