Expand description
xxHash — extremely fast non-cryptographic hash algorithm.
Provides four variants:
| Struct | Bits | Description |
|---|---|---|
Xxh3_64 | 64 | Recommended — XXH3 algorithm, faster on modern CPUs |
Xxh3_128 | 128 | Recommended — XXH3 algorithm, 128-bit output |
Xxh32 | 32 | Classic xxHash (XXH32) |
Xxh64 | 64 | Classic 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)
stdfeature 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 enabledtarget-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.
- Checksum
Output - Trait for types that can be used as xxHash outputs.