Expand description
§WyRand-rs
A fast & portable non-cryptographic pseudorandom number generator written in Rust, and optionally, the hashing algorithm as well.
The implementations for both the PRNG and hasher are based on the C reference implementation wyhash, a simple and fast hasher but not cryptographically secure. It’s known to be extremely fast and performant while still having great statistical properties.
This crate provides both the v4.2 final implementation of the WyRand/WyHash algorithm, or the older final v4 implementation. The two versions have different outputs due to changes in the algorithm and also with the constants used. By default, the final v4.2 algorithm will be used. If one needs to use the older, legacy v4 implementation for compatibility/stability reasons, the legacy hasher and PRNG can be exposed by enabling the legacy_v4
feature flag.
This crate can be used on its own or be integrated with rand_core
/rand
, and it is no-std
compatible. Minimum compatible Rust version is 1.70. This crate is also implemented with no unsafe code via #![forbid(unsafe_code)]
.
§Example
Generate a random value:
use wyrand::WyRand;
// Provide a seed to the PRNG
let mut rng = WyRand::new(Default::default());
let value = rng.rand();
§Features
The crate will always export WyRand
and will do so when set as default-features = false
in the Cargo.toml. By default, it will have the rand_core
, debug
features enabled.
rand_core
- Enables support forrand_core
, implementingRngCore
&SeedableRng
onWyRand
.debug
- Enablescore::fmt::Debug
implementation forWyRand
/WyHash
.serde1
- EnablesSerialize
andDeserialize
derives onWyRand
.hash
- Enablescore::hash::Hash
implementation forWyRand
.wyhash
- EnablesWyHash
, a fast & portable hashing algorithm. Based on the final v4 C implementation.randomised_wyhash
- EnablesRandomWyHashState
, a means to source a randomised state forWyHash
for use in collections likeHashMap
/HashSet
. Enableswyhash
feature if it is not already enabled.legacy_v4
- Exposes the legacy PRNG/Hashing algorithms that use the final v4 implementation.
Below are application only features, meant only to be enabled by app/bin crates, NOT lib crates as this changes runtime behaviour and also can pull in crates that change whether this crate can compile for no-std
environments or not:
fully_randomised_wyhash
- Randomises not just the seed forRandomWyHashState
, but also the secret. The new secret is generated once per runtime, and then is used for every subsequent newWyHash
(with eachWyHash
instance having its own unique seed). Enablesrandomised_wyhash
if not already enabled, and requiresstd
environments. ONLY FOR BIN CRATES.threadrng_wyhash
- Enables sourcing entropy fromrand
’sthread_rng()
method. Much quicker thangetrandom
. Enablesrandomised_wyhash
if not already enabled. Requiresstd
environments. ONLY FOR BIN CRATES.
§Building for WASM/Web
If you are using WyRand
with rand_core
and/or WyHash
with randomised_wyhash
then for building for the web/WASM, you’ll need to configure getrandom
and its backend to make use of the browser APIs in order to source entropy from. If your WASM builds target the web, enable the wasm_js
feature for getrandom
in Cargo.toml
:
[target.'cfg(all(target_arch = "wasm32", target_os = "unknown"))'.dependencies]
getrandom = { version = "0.3", features = ["wasm_js"] }
and then add the following to your project .cargo/config.toml
:
[target.wasm32-unknown-unknown]
rustflags = ['--cfg', 'getrandom_backend="wasm_js"']
§License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Modules§
- legacy_
final_ v4 legacy_v4
- Legacy final v4 implementations of
WyRand
&WyHash
. These are the older legacy version of the algorithms, only use them if for whatever reason you favour stability with previous usages over to switching to more secure versions of the algorithm.
Structs§
- Random
WyHash State randomised_wyhash
- Randomised state constructor for
WyHash
. This builder will source entropy in order to provide random seeds forWyHash
. If thefully_randomised_wyhash
feature is enabled, this will yield a hasher with not just a random seed, but also a new random secret, granting extra protection against DOS and prediction attacks. - Secret
wyhash
- A wrapper struct for containing generated secrets to be used by the wyhash algorithm. Ensures it can’t be used
incorrectly, and can only be constructed by
super::WyHash::make_secret
. - WyHash
wyhash
- The WyHash hasher, a fast & portable hashing algorithm. This implementation is based on the final v4.2 C reference implementation.
- WyRand
- A Pseudorandom Number generator, powered by the
wyrand
algorithm. This generator is based on the final v4.2 reference implementation.