Function ssdeep::compare

source ·
pub fn compare(hash1: &str, hash2: &str) -> Result<i8>
Expand description

Computes the match score between two fuzzy hashes.

Returns a value from 0 to 100 indicating the match score of the two hashes. A match score of zero indicates that the hashes did not match. When an error occurs, it returns Error.

Examples

When the hashes are identical, it returns 100:

let h1 = "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C";
let h2 = "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C";
assert_eq!(ssdeep::compare(h1, h2), Ok(100));

When the hashes are similar, it returns a positive integer:

let h1 = "3:AXGBicFlgVNhBGcL6wCrFQEv:AXGHsNhxLsr2C";
let h2 = "3:AXGBicFlIHBGcL6wCrFQEv:AXGH6xLsr2Cx";
assert_eq!(ssdeep::compare(h1, h2), Ok(22));

When the hashes have no similarity at all, it returns zero:

let h1 = "3:u+N:u+N";
let h2 = "3:OWIXTn:OWQ";
assert_eq!(ssdeep::compare(h1, h2), Ok(0));

When either of the hashes is invalid, it returns an error:

let h1 = "XYZ";
let h2 = "3:tc:u";
assert_eq!(
    ssdeep::compare(h1, h2),
    Err(ssdeep::Error::CFunctionFailed {
        name: "fuzzy_compare".to_string(),
        return_code: -1,
    })
);

Panics

If either of the hashes contain a null byte. Note that hash() never returns a hash with a null byte, so this may happen only if you handcrafted the hashes or obtained them from other sources.

Implementation details

Internally, it calls the fuzzy_compare() function from the underlying C library. The return value -1 is translated into Error.