Skip to main content

Crate xxhash

Crate xxhash 

Source
Expand description

xxHash — extremely fast non-cryptographic hash algorithm.

Provides four variants:

StructBitsDescription
Xxh3_6464Recommended — XXH3 algorithm, faster on modern CPUs
Xxh3_128128Recommended — XXH3 algorithm, 128-bit output
Xxh3232Classic xxHash (XXH32)
Xxh6464Classic xxHash (XXH64)

Prefer Xxh3_64 or Xxh3_128 for new code. XXH3 is the modern variant and is around twice as fast as the classic XXH64 on large inputs, with better hash quality.

All types implement the Checksum trait. Incremental hashing agrees with one-shot hashing for every input length and every split of the input across update calls, and no input can make any of these functions panic.

Results are identical on every platform and match the reference implementation.

§SIMD acceleration

The long-input path of XXH3 is accelerated with NEON (aarch64), AVX2 / AVX-512 (x86) or SIMD128 (wasm32) kernels:

  • With the (default) std feature enabled, the kernel is chosen at runtime from the CPU features actually available, so a binary compiled for a baseline target still uses the widest instructions the machine supports.
  • With default-features = false (i.e. no_std), the kernel is chosen at compile time from the enabled target-features; pass e.g. RUSTFLAGS="-C target-feature=+avx2" to enable them.

The const one-shot functions are scalar-only, so prefer Checksum::checksum or Checksum::update for long inputs when runtime performance matters. Both produce the same value.

§Security

xxHash is not a cryptographic hash. It must not be used to authenticate messages, derive keys, or store passwords, and a seed or a custom secret is not a security boundary. Its collision resistance is not sufficient to protect a hash table from attacker-chosen keys (hash flooding): XXH3 in particular has known collision weaknesses for 17..=240 byte inputs. For those uses, prefer a keyed cryptographic hash.

§Const one-shot hashing

Use the free functions xxh32, xxh64, xxh3_64, and xxh3_128 for const-compatible one-shot hashing:

use xxhash::xxh3_64;

const HASH: u64 = xxh3_64(b"hello");
assert_eq!(HASH, 0x9555E8555C62DCFD);

§Examples

§One-shot hashing (via trait)

use xxhash::{Xxh3_64, Checksum};

let hash: u64 = Xxh3_64::checksum(b"hello world");

§Incremental hashing

use xxhash::{Xxh3_128, Checksum};

let mut hasher = Xxh3_128::new();
hasher.update(b"hello ");
hasher.update(b"world");
let hash: u128 = hasher.sum();

§Seeded hashing

use xxhash::{Xxh3_64, Checksum};

let mut hasher = Xxh3_64::with_seed(42);
hasher.update(b"data");
let hash: u64 = hasher.sum();

§XXH3 with a custom secret

use xxhash::{Xxh3_64, Checksum};

let secret = [0xAB; 192];
let mut hasher = Xxh3_64::with_secret(secret);
hasher.update(b"data");
let hash: u64 = hasher.sum();

Structs§

Xxh3_64
XXH3 64-bit hash.
Xxh3_128
XXH3 128-bit hash.
Xxh32
XXH32 hash (32-bit).
Xxh64
XXH64 hash (64-bit).

Traits§

Checksum
A trait for computing checksums.
ChecksumOutput
Trait for types that can be used as xxHash outputs.

Functions§

xxh3_64
Compute the XXH3 64-bit hash of data in a single call.
xxh3_128
Compute the XXH3 128-bit hash of data in a single call.
xxh32
Compute the XXH32 hash of data in a single call.
xxh64
Compute the XXH64 hash of data in a single call.