legit-hashes 0.2.1

Versioned similarity hashes of images and text, and MinHash set signatures, with index keys for finding near matches
Documentation
//! Similarity hashes of images and text that stay the same once stored: each algorithm is a
//! written, versioned definition whose output never changes.
//!
//! A plain Rust library with no framework dependency. It computes a [`Hash`](struct@Hash),
//! compares two with [`Hash::distance`], and splits one into [`Hash::chunks`], index keys for
//! finding near matches in a database.
//!
//! - Images: [`average_v1`], [`difference_v1`] and [`perceptual_v1`] hash a [`Luma`] image, an
//!   8-bit greyscale view; [`LumaBuf::from_rgb8_v1`] converts RGB. [`pdq_v1`], Meta's PDQ, and
//!   [`blockhash_v1`] hash an [`Rgba`] image into 256 bits; [`RgbaBuf::from_rgb8`] makes one from
//!   RGB. With the `image` feature, [`decode_luma_v1`], [`decode_rgba_v1`] and
//!   [`Algorithm::hash_encoded`] read PNG and JPEG files.
//! - Text: [`simhash_v1`] hashes weighted features, such as the words from [`words_v1`] or the
//!   shingles from [`shingles_v1`].
//! - Sets: [`minhash_v1`] makes a [`Signature`], 128 values that estimate how much two sets of
//!   features overlap, with [`Signature::band_keys`] to find probable near matches;
//!   [`minhash_v1_shingles`] makes one from the shingles of a text. [`minhash_legacy_v1`] and
//!   [`minhash_legacy_v1_shingles`] make the signatures datasketch made by default before its
//!   version 2.0.0, to agree with those.
//!
//! ```
//! use legit_hashes::{Hash, Luma, perceptual_v1, simhash_v1, words_v1};
//!
//! let pixels: Vec<u8> = (0..64 * 48).map(|i| (i % 64 * 4) as u8).collect();
//! let image = perceptual_v1(Luma::new(64, 48, &pixels)?);
//!
//! let text = simhash_v1(words_v1("A plain Rust library").map(|word| (word, 1)));
//!
//! // Store the string form; it names the algorithm and its version...
//! let stored = image.to_string();
//! assert!(stored.starts_with("perceptual-v1:"));
//! assert_eq!(stored.parse::<Hash>()?.distance(&image)?, 0);
//!
//! // ...and hashes of different algorithms don't compare.
//! assert!(image.distance(&text).is_err());
//! # Ok::<(), legit_hashes::Error>(())
//! ```
//!
//! Every hashing function and [`Algorithm`] variant names its version. A released definition never
//! changes its output; a change ships as a new version beside the old one. Nothing is called
//! "latest" or "default", so upgrading the crate never changes what an application stores.
//!
//! Features:
//!
//! - `image`: [`decode_luma_v1`], [`decode_rgba_v1`] and [`Algorithm::hash_encoded`], decoding PNG
//!   and JPEG with the `image` crate.
//! - `serde`: `Serialize` and `Deserialize` for [`Hash`](struct@Hash), [`Algorithm`],
//!   [`Signature`] and [`SignatureAlgorithm`], as their string forms.

#![forbid(unsafe_code)]

mod algorithm;
mod blockhash;
#[cfg(feature = "image")]
mod decode;
mod error;
mod hash;
mod image_hash;
mod luma;
mod minhash;
mod minhash_legacy;
mod pdq;
mod perceptual;
mod rgba;
#[cfg(feature = "serde")]
mod serde_support;
mod signature;
mod simhash;
#[cfg_attr(
    all(any(target_arch = "x86_64", target_arch = "aarch64"), not(test)),
    expect(
        dead_code,
        reason = "these targets hash with `f32`, whose arithmetic is IEEE-754 there"
    )
)]
mod strict_f32;

pub use algorithm::Algorithm;
pub use blockhash::blockhash_v1;
#[cfg(feature = "image")]
pub use decode::{DECODE_MAX_ALLOC, DECODE_MAX_DIMENSION, DECODE_MAX_INPUT, decode_luma_v1, decode_rgba_v1};
pub use error::{Error, Result};
pub use hash::Hash;
pub use image_hash::{average_v1, difference_v1};
pub use luma::{Luma, LumaBuf, MAX_DIMENSION, luma_v1};
pub use minhash::{MinHashV1, minhash_v1, minhash_v1_shingles};
pub use minhash_legacy::{MinHashLegacyV1, minhash_legacy_v1, minhash_legacy_v1_shingles};
pub use pdq::{Pdq, pdq_v1, pdq_v1_luma};
pub use perceptual::perceptual_v1;
pub use rgba::{Rgba, RgbaBuf};
pub use signature::{Signature, SignatureAlgorithm};
pub use simhash::{ShinglesV1, SimHashV1, WordsV1, shingles_v1, simhash_v1, words_v1};

/// The README's examples, compiled and run as doctests. One reads a file, so they need `image`.
#[cfg(all(doctest, feature = "image"))]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;