Skip to main content

Crate legit_hashes

Crate legit_hashes 

Source
Expand description

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, compares two with Hash::distance, and splits one into Hash::chunks, index keys for finding near matches in a database.

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());

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:

Structs§

Hash
A hash: the algorithm that made it, version included, and its bits.
Luma
A borrowed 8-bit luma image: one byte a pixel, row by row from the top left.
LumaBuf
An owned 8-bit luma image, such as one converted from RGB. view borrows it for hashing.
MinHashLegacyV1
A minhash-legacy-v1 signature being built a feature at a time, for features that arrive in parts.
MinHashV1
A minhash-v1 signature being built a feature at a time, for features that arrive in parts.
Pdq
A pdq-v1 hash and the quality score the reference computes with it.
Rgba
A borrowed 8-bit RGBA image: four bytes a pixel, red, green, blue and alpha, row by row from the top left. Alpha 0 is fully transparent and 255 opaque; the colour isn’t premultiplied.
RgbaBuf
An owned 8-bit RGBA image, such as one made from RGB or greyscale pixels. view borrows it for hashing.
ShinglesV1
The shingles of a text, from shingles_v1.
Signature
A set signature: the algorithm that made it, version included, and its values.
SimHashV1
A simhash-v1 hash being built a feature at a time, for features that arrive in parts.
WordsV1
The words of a text, from words_v1.

Enums§

Algorithm
A hash algorithm at one version of its definition.
Error
What went wrong. Every failure in this crate, bad input included, is one of these; nothing panics.
SignatureAlgorithm
A set-signature algorithm at one version of its definition.

Constants§

DECODE_MAX_ALLOC
The most memory the decoder may allocate for one file: 256 MiB.
DECODE_MAX_DIMENSION
The widest or tallest file decode_luma_v1 decodes: 16,384 pixels.
DECODE_MAX_INPUT
The longest file decode_luma_v1 reads: 64 MiB. The JPEG decoder copies its whole input before it applies any other limit, so a longer file is refused before decoding starts.
MAX_DIMENSION
The largest width or height an image may have: 65,535 pixels.

Functions§

average_v1
Hash an image with average-v1, the average hash.
blockhash_v1
Hash an image with blockhash-v1, the block mean value hash.
decode_luma_v1
Decode a PNG or JPEG file and convert it to luma with luma_v1.
decode_rgba_v1
Decode a PNG or JPEG file to 8-bit RGBA, the input of pdq-v1 and blockhash-v1.
difference_v1
Hash an image with difference-v1, the difference hash.
luma_v1
Convert one RGB pixel to luma with the v1 formula.
minhash_legacy_v1
Make the minhash-legacy-v1 signature of a set of features, Broder’s MinHash as the Python library datasketch computed it by default before its version 2.0.0.
minhash_legacy_v1_shingles
Make the minhash-legacy-v1 signature of the character shingles of text, from shingles_v1 with size. minhash_v1_shingles says how to choose the size and when to normalise the text first.
minhash_v1
Make the minhash-v1 signature of a set of features, Broder’s MinHash as the Python library datasketch computes it by default since its version 2.0.0.
minhash_v1_shingles
Make the minhash-v1 signature of the character shingles of text, from shingles_v1 with size.
pdq_v1
Hash an image with pdq-v1, Meta’s PDQ, and score its quality.
pdq_v1_luma
Hash a greyscale image with pdq-v1, taking each grey level as the luma.
perceptual_v1
Hash an image with perceptual-v1, the DCT hash.
shingles_v1
Split text into shingles of size characters with the frozen shingles-v1 tokeniser.
simhash_v1
Hash weighted features with simhash-v1, Charikar’s SimHash.
words_v1
Split text into words with the frozen words-v1 tokeniser.

Type Aliases§

Result
The result of a fallible operation in this crate.