Skip to content

Commit

Permalink
chore: remove OsRng
Browse files Browse the repository at this point in the history
BREAKING CHANGE: public api changed
  • Loading branch information
iancoleman committed Mar 11, 2022
1 parent e579c7e commit c4d64e9
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,8 @@ use core::{cmp::Ordering, fmt, ops};
pub use prefix::Prefix;
use rand::{
distributions::{Distribution, Standard},
rngs::OsRng,
Rng,
};
use rand_core::RngCore;
use serde::{Deserialize, Serialize};
use tiny_keccak::{Hasher, Sha3};

Expand Down Expand Up @@ -128,11 +126,9 @@ impl XorName {
}

/// Generate a random XorName
pub fn random() -> Self {
pub fn random<T: Rng>(rng: &mut T) -> Self {
let mut xor = [0u8; XOR_NAME_LEN];
// TODO: OsRng needs to be removed + replaced to follow no-std.
OsRng.fill_bytes(&mut xor);

rng.fill(&mut xor);
Self(xor)
}

Expand Down Expand Up @@ -337,15 +333,17 @@ mod tests {

#[test]
fn create_random_xorname() {
let xorname: XorName = XorName::random();
let xorname2: XorName = XorName::random();
let mut rng = SmallRng::from_entropy();
let xorname: XorName = XorName::random(&mut rng);
let xorname2: XorName = XorName::random(&mut rng);

assert_ne!(xorname, xorname2);
}

#[test]
fn serialisation_xor_name() {
let obj_before: XorName = XorName::random();
let mut rng = SmallRng::from_entropy();
let obj_before: XorName = XorName::random(&mut rng);
let data = serialize(&obj_before).unwrap();
assert_eq!(data.len(), XOR_NAME_LEN);
let obj_after: XorName = deserialize(&data).unwrap();
Expand Down

0 comments on commit c4d64e9

Please sign in to comment.