diff --git a/Cargo.toml b/Cargo.toml index b759d2d..8bb4d59 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,10 +23,12 @@ serde1 = ["dep:serde"] wyhash = [] randomised_wyhash = ["wyhash", "dep:getrandom"] fully_randomised_wyhash = ["randomised_wyhash"] +threadrng_wyhash = ["dep:rand", "randomised_wyhash"] v4_2 = [] [dependencies] getrandom = { version = "0.2", optional = true } +rand = { version = "0.8", optional = true } rand_core = { version = "0.6", default-features = false, optional = true } serde = { version = "1.0", features = ["derive"], optional = true } diff --git a/README.md b/README.md index c2de2f6..2af5339 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,7 @@ The crate will always export `WyRand` and will do so when set as `default-featur - **`wyhash`** - Enables `WyHash`, a fast & portable hashing algorithm. Based on the final v4 C implementation. - **`randomised_wyhash`** - Enables `RandomisedWyHashBuilder`, a means to source a randomised state for `WyHash` for use in collections like `HashMap`/`HashSet`. Enables `wyhash` feature if it is not already enabled. - **`fully_randomised_wyhash`** - Randomises not just the seed for `RandomisedWyHashBuilder`, but also the secret. Incurs a performance hit every time `WyHash` is initialised but it is more secure as a result. Enables `randomised_wyhash` if not already enabled. +- **`threadrng_wyhash`** - Enables sourcing entropy from `rand`'s `thread_rng()` method. Much quicker than `getrandom` and best used without the `fully_randomised_wyhash` flag as the overhead of calculating new secrets dwarfs any gains in entropy sourcing. Enables `randomised_wyhash` if not already enabled. Requires `std` environments. - **`v4_2`** - Switches the PRNG/Hashing algorithms to use the final v4.2 implementation. ## Building for WASM/Web diff --git a/src/hasher/builder.rs b/src/hasher/builder.rs index 95999c8..2c8a3e0 100644 --- a/src/hasher/builder.rs +++ b/src/hasher/builder.rs @@ -46,8 +46,17 @@ impl RandomWyHashState { let mut state = [0; SIZE]; - getrandom::getrandom(&mut state) - .expect("Failed to source entropy for WyHash randomised state"); + #[cfg(not(feature = "threadrng_wyhash"))] + { + getrandom::getrandom(&mut state) + .expect("Failed to source entropy for WyHash randomised state"); + } + #[cfg(feature = "threadrng_wyhash")] + { + use rand::RngCore; + + rand::thread_rng().fill_bytes(&mut state); + } Self { state } } diff --git a/src/lib.rs b/src/lib.rs index 7d4b78d..43ac0d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,9 +8,9 @@ mod constants; #[cfg(feature = "wyhash")] mod hasher; -mod rand; +mod wyrand; mod utils; #[cfg(feature = "wyhash")] pub use hasher::*; -pub use rand::WyRand; +pub use wyrand::WyRand; diff --git a/src/rand.rs b/src/wyrand.rs similarity index 100% rename from src/rand.rs rename to src/wyrand.rs