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.
- Images:
average_v1,difference_v1andperceptual_v1hash aLumaimage, an 8-bit greyscale view;LumaBuf::from_rgb8_v1converts RGB.pdq_v1, Meta’s PDQ, andblockhash_v1hash anRgbaimage into 256 bits;RgbaBuf::from_rgb8makes one from RGB. With theimagefeature,decode_luma_v1,decode_rgba_v1andAlgorithm::hash_encodedread PNG and JPEG files. - Text:
simhash_v1hashes weighted features, such as the words fromwords_v1or the shingles fromshingles_v1. - Sets:
minhash_v1makes aSignature, 128 values that estimate how much two sets of features overlap, withSignature::band_keysto find probable near matches;minhash_v1_shinglesmakes one from the shingles of a text.minhash_legacy_v1andminhash_legacy_v1_shinglesmake 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());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_v1andAlgorithm::hash_encoded, decoding PNG and JPEG with theimagecrate.serde:SerializeandDeserializeforHash,Algorithm,SignatureandSignatureAlgorithm, as their string forms.
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.
viewborrows it for hashing. - MinHash
Legacy V1 - A
minhash-legacy-v1signature being built a feature at a time, for features that arrive in parts. - MinHash
V1 - A
minhash-v1signature being built a feature at a time, for features that arrive in parts. - Pdq
- A
pdq-v1hash 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.
viewborrows it for hashing. - Shingles
V1 - The shingles of a text, from
shingles_v1. - Signature
- A set signature: the algorithm that made it, version included, and its values.
- SimHash
V1 - A
simhash-v1hash 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.
- Signature
Algorithm - 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_v1decodes: 16,384 pixels. - DECODE_
MAX_ INPUT - The longest file
decode_luma_v1reads: 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-v1andblockhash-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-v1signature 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-v1signature of the character shingles oftext, fromshingles_v1withsize.minhash_v1_shinglessays how to choose the size and when to normalise the text first. - minhash_
v1 - Make the
minhash-v1signature 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-v1signature of the character shingles oftext, fromshingles_v1withsize. - 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
textinto shingles ofsizecharacters with the frozenshingles-v1tokeniser. - simhash_
v1 - Hash weighted features with
simhash-v1, Charikar’s SimHash. - words_
v1 - Split
textinto words with the frozenwords-v1tokeniser.
Type Aliases§
- Result
- The result of a fallible operation in this crate.