#digest #hash #similarity #tlsh #hashing

tlsh2

A rust implementation of the TLSH algorithm

4 releases (2 breaking)

0.3.0 Jul 30, 2023
0.2.1 May 4, 2023
0.2.0 Jan 1, 2023
0.1.0 Dec 30, 2022

#1465 in Algorithms

Download history 5/week @ 2024-01-08 44/week @ 2024-01-15 122/week @ 2024-01-22 173/week @ 2024-01-29 150/week @ 2024-02-05 166/week @ 2024-02-12 58/week @ 2024-02-19 59/week @ 2024-02-26 229/week @ 2024-03-04 48/week @ 2024-03-11 128/week @ 2024-03-18 47/week @ 2024-03-25 139/week @ 2024-04-01 359/week @ 2024-04-08 115/week @ 2024-04-15 192/week @ 2024-04-22

809 downloads per month
Used in boreal

Apache-2.0 OR BSD-3-Clause

520KB
3.5K SLoC

TLSH2

Build status Crates.io Documentation

Rust port of the TLSH library. The code is kept close to the original C++ version, to limit bugs and help maintainability

This crate is no_std and different configurations of bucket numbers and checksum length are handled as generics, making every configuration properly optimized.

// The default builder uses 128 buckets and a 1-byte checksum.
// Other builders are also available.
let mut builder = tlsh2::TlshDefaultBuilder::new();
builder.update(b"Sed ut perspiciatis unde omnis iste natus");
builder.update(b"error sit voluptatem accusantium");
let tlsh = builder.build()
    .ok_or_else(|| "could not generate TLSH from payload")?;

// Alternatively, a TLSH object can be generated directly from
// a byte slice.
let tlsh2 = tlsh2::TlshDefaultBuilder::build_from(
    b"odit aut fugit, sed quia consequuntur magni dolores"
).ok_or_else(|| "could not generate TLSH from second payload")?;

// Then, the TLSH object can be used to generated a hash or compute
// distances
assert_eq!(
    tlsh.hash(),
    b"T184A022B383C2A2A20ACB0830880CF0202CCAC080033A023800338\
      A30B0880AA8E0BE38".as_slice(),
);
// The `diff` feature is required for this computation.
assert_eq!(tlsh.diff(&tlsh2, true), 209);

Those configurations are available:

  • 128 buckets and 1-byte checksum (default).
  • 128 buckets and 3-byte checksum.
  • 256 buckets and 1-byte checksum.
  • 256 buckets and 3-byte checksum.
  • 48 buckets and 1-byte checksum.

The threaded and private options that exists in the original TLSH version are not yet implemented.

No runtime deps