#xx-hash #hasher #hash #xxh3

no-std twox-hash

A Rust implementation of the XXHash and XXH3 algorithms

15 stable releases

new 2.0.1 Nov 4, 2024
1.6.3 May 4, 2022
1.6.2 Dec 25, 2021
1.6.1 Aug 5, 2021
0.1.1 Jul 20, 2015

#3 in Algorithms

Download history 623540/week @ 2024-07-18 652611/week @ 2024-07-25 627963/week @ 2024-08-01 651487/week @ 2024-08-08 663466/week @ 2024-08-15 661722/week @ 2024-08-22 607847/week @ 2024-08-29 673366/week @ 2024-09-05 640734/week @ 2024-09-12 640531/week @ 2024-09-19 674998/week @ 2024-09-26 693412/week @ 2024-10-03 697148/week @ 2024-10-10 696370/week @ 2024-10-17 705446/week @ 2024-10-24 659225/week @ 2024-10-31

2,902,498 downloads per month
Used in 2,017 crates (188 directly)

MIT license

115KB
2.5K SLoC

A Rust implementation of the xxHash algorithm.

Crates.io Documentation Build Status

Examples

These examples use XxHash64 but the same ideas can be used for XxHash32 or XxHash3_64.

Hashing arbitrary data

When all the data is available at once

use twox_hash::XxHash64;

let seed = 1234;
let hash = XxHash64::oneshot(seed, b"some bytes");
assert_eq!(0xeab5_5659_a496_d78b, hash);

When the data is streaming

use std::hash::Hasher as _;
use twox_hash::XxHash64;

let seed = 1234;
let mut hasher = XxHash64::with_seed(seed);
hasher.write(b"some");
hasher.write(b" ");
hasher.write(b"bytes");
let hash = hasher.finish();
assert_eq!(0xeab5_5659_a496_d78b, hash);

In a HashMap

With a default seed

use std::{collections::HashMap, hash::BuildHasherDefault};
use twox_hash::XxHash64;

let mut hash = HashMap::<_, _, BuildHasherDefault<XxHash64>>::default();
hash.insert(42, "the answer");
assert_eq!(hash.get(&42), Some(&"the answer"));

With a random seed

use std::collections::HashMap;
use twox_hash::xxhash64;

let mut hash = HashMap::<_, _, xxhash64::RandomState>::default();
hash.insert(42, "the answer");
assert_eq!(hash.get(&42), Some(&"the answer"));

With a fixed seed

use std::collections::HashMap;
use twox_hash::xxhash64;

let mut hash = HashMap::with_hasher(xxhash64::State::with_seed(0xdead_cafe));
hash.insert(42, "the answer");
assert_eq!(hash.get(&42), Some(&"the answer"));

Feature Flags

name description
xxhash32 Include the XxHash32 algorithm
xxhash64 Include the XxHash64 algorithm
xxhash3_64 Include the XxHash3_64 algorithm
random Create random instances of the hashers
serialize Serialize and deserialize hasher state with Serde
std Use the Rust standard library. Enable this if you want SIMD support in XxHash3_64
alloc Use the Rust allocator library. Enable this if you want to create XxHash3_64 with dynamic secrets

Benchmarks

See benchmarks in the comparison README.

Contributing

  1. Fork it (https://github.com/shepmaster/twox-hash/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Add a failing test.
  4. Add code to pass the test.
  5. Commit your changes (git commit -am 'Add some feature')
  6. Ensure tests pass.
  7. Push to the branch (git push origin my-new-feature)
  8. Create a new Pull Request

Dependencies

~0–500KB