Skip to main content

legit_hashes/
lib.rs

1//! Similarity hashes of images and text that stay the same once stored: each algorithm is a
2//! written, versioned definition whose output never changes.
3//!
4//! A plain Rust library with no framework dependency. It computes a [`Hash`](struct@Hash),
5//! compares two with [`Hash::distance`], and splits one into [`Hash::chunks`], index keys for
6//! finding near matches in a database.
7//!
8//! - Images: [`average_v1`], [`difference_v1`] and [`perceptual_v1`] hash a [`Luma`] image, an
9//!   8-bit greyscale view; [`LumaBuf::from_rgb8_v1`] converts RGB. [`pdq_v1`], Meta's PDQ, and
10//!   [`blockhash_v1`] hash an [`Rgba`] image into 256 bits; [`RgbaBuf::from_rgb8`] makes one from
11//!   RGB. With the `image` feature, [`decode_luma_v1`], [`decode_rgba_v1`] and
12//!   [`Algorithm::hash_encoded`] read PNG and JPEG files.
13//! - Text: [`simhash_v1`] hashes weighted features, such as the words from [`words_v1`] or the
14//!   shingles from [`shingles_v1`].
15//! - Sets: [`minhash_v1`] makes a [`Signature`], 128 values that estimate how much two sets of
16//!   features overlap, with [`Signature::band_keys`] to find probable near matches;
17//!   [`minhash_v1_shingles`] makes one from the shingles of a text. [`minhash_legacy_v1`] and
18//!   [`minhash_legacy_v1_shingles`] make the signatures datasketch made by default before its
19//!   version 2.0.0, to agree with those.
20//!
21//! ```
22//! use legit_hashes::{Hash, Luma, perceptual_v1, simhash_v1, words_v1};
23//!
24//! let pixels: Vec<u8> = (0..64 * 48).map(|i| (i % 64 * 4) as u8).collect();
25//! let image = perceptual_v1(Luma::new(64, 48, &pixels)?);
26//!
27//! let text = simhash_v1(words_v1("A plain Rust library").map(|word| (word, 1)));
28//!
29//! // Store the string form; it names the algorithm and its version...
30//! let stored = image.to_string();
31//! assert!(stored.starts_with("perceptual-v1:"));
32//! assert_eq!(stored.parse::<Hash>()?.distance(&image)?, 0);
33//!
34//! // ...and hashes of different algorithms don't compare.
35//! assert!(image.distance(&text).is_err());
36//! # Ok::<(), legit_hashes::Error>(())
37//! ```
38//!
39//! Every hashing function and [`Algorithm`] variant names its version. A released definition never
40//! changes its output; a change ships as a new version beside the old one. Nothing is called
41//! "latest" or "default", so upgrading the crate never changes what an application stores.
42//!
43//! Features:
44//!
45//! - `image`: [`decode_luma_v1`], [`decode_rgba_v1`] and [`Algorithm::hash_encoded`], decoding PNG
46//!   and JPEG with the `image` crate.
47//! - `serde`: `Serialize` and `Deserialize` for [`Hash`](struct@Hash), [`Algorithm`],
48//!   [`Signature`] and [`SignatureAlgorithm`], as their string forms.
49
50#![forbid(unsafe_code)]
51
52mod algorithm;
53mod blockhash;
54#[cfg(feature = "image")]
55mod decode;
56mod error;
57mod hash;
58mod image_hash;
59mod luma;
60mod minhash;
61mod minhash_legacy;
62mod pdq;
63mod perceptual;
64mod rgba;
65#[cfg(feature = "serde")]
66mod serde_support;
67mod signature;
68mod simhash;
69#[cfg_attr(
70    all(any(target_arch = "x86_64", target_arch = "aarch64"), not(test)),
71    expect(
72        dead_code,
73        reason = "these targets hash with `f32`, whose arithmetic is IEEE-754 there"
74    )
75)]
76mod strict_f32;
77
78pub use algorithm::Algorithm;
79pub use blockhash::blockhash_v1;
80#[cfg(feature = "image")]
81pub use decode::{DECODE_MAX_ALLOC, DECODE_MAX_DIMENSION, DECODE_MAX_INPUT, decode_luma_v1, decode_rgba_v1};
82pub use error::{Error, Result};
83pub use hash::Hash;
84pub use image_hash::{average_v1, difference_v1};
85pub use luma::{Luma, LumaBuf, MAX_DIMENSION, luma_v1};
86pub use minhash::{MinHashV1, minhash_v1, minhash_v1_shingles};
87pub use minhash_legacy::{MinHashLegacyV1, minhash_legacy_v1, minhash_legacy_v1_shingles};
88pub use pdq::{Pdq, pdq_v1, pdq_v1_luma};
89pub use perceptual::perceptual_v1;
90pub use rgba::{Rgba, RgbaBuf};
91pub use signature::{Signature, SignatureAlgorithm};
92pub use simhash::{ShinglesV1, SimHashV1, WordsV1, shingles_v1, simhash_v1, words_v1};
93
94/// The README's examples, compiled and run as doctests. One reads a file, so they need `image`.
95#[cfg(all(doctest, feature = "image"))]
96#[doc = include_str!("../README.md")]
97struct ReadmeDoctests;