8 releases (3 stable)

1.0.2 Nov 17, 2023
0.1.4 Nov 17, 2023

#115 in No standard library

29 downloads per month

MPL-2.0 license

12KB
181 lines

rand-blake3

Implementation of RngCore using BLAKE3 as a PRNG.

Example

// Hash input and convert the output stream to an rng.
let mut hasher = blake3::Hasher::new();
hasher.update(b"foo");
hasher.update(b"bar");
hasher.update(b"baz");
let mut rng: rand_blake3::Rng = hasher.into();
let output: u64 = rng.gen();
assert_eq!(output, 0xfb61f3c9e0fe9ac0u64);

// Alternately, seed it as a rand::SeedableRng.
let mut rng = rand_blake3::Rng::from_seed(*b"0123456789abcdefghijklmnopqrstuv");
let output: u64 = rng.gen();
assert_eq!(output, 0x9958c58595366357u64);

// In the real world, you will probably not use a static seed, but seed from
// OsRng or something of the sort.
let mut rng = rand_blake3::Rng::from_entropy();
let _output: u64 = rng.gen();

// You can also use this as a backing for a rand ReseedingRng
let mut reseeding = rand::rngs::adapter::ReseedingRng::new(
    rand_blake3::UnbufferedRng::from_entropy(),
    1024 * 256,
    rand::rngs::OsRng,
);
let _output: u64 = reseeding.gen();

Dependencies

~1.5MB
~44K SLoC