Skip to content

wyhash fast portable non-cryptographic hashing algorithm and random number generator in Rust

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT
Notifications You must be signed in to change notification settings

eldruin/wyhash-rs

Repository files navigation

wyhash fast portable non-cryptographic hashing algorithm

crates.io Docs Build Status Coverage Status

Rust implementation of the wyhash algorithm by Wang Yi.

The hashing algorithm passes SMHasher and the random number generator passes BigCrush and practrand. As of now it is the fastest algorithm in the SMHasher benchmark (faster than t1ha and XXH3). See here.

Furthermore, this algorithm is solid, simple, portable (does not need hardware support, can be used in no_std environments) and has no dependencies (except the traits from rand_core).

The generated hashes are equal (see tests) as of the version stated here although the speed varies (PRs are welcome).

Crate features

By default this crate uses 128-bit integer multiplications. To restrict that to 64 bits you can enable the feature mum32bit. This offers better performance on 32-bit architectures. Beware that this feature produces different results.

Usage

This crate provides free functions as well as implementations of the Hasher, Rng and SeedableRng traits.

use core::hash::Hasher;
use wyhash::{make_secret, WyHash};

fn main() {
    let secret = make_secret(3);
    let mut hasher = WyHash::new(4, secret);
    hasher.write(&[0, 1, 2]);
    assert_eq!(0x8301_42a2_4404_5ff4, hasher.finish());
}

See further examples of the hasher and RNG in the documentation.

Performance comparison

A basic performance comparison benchmark against fnv, twox-hash, metrohash and the standard hash_map hash collections::hash_map::DefaultHasher is included in the sources and you can run it with nightly Rust:

cargo +nightly bench

Support

For questions, issues, feature requests, and other changes, please file an issue in the github project.

Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.36.0 and up. It might compile with older versions but that may change in any new patch release.

License

Licensed under either of

at your option.

Contributing

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.