Skip to content

Commit

Permalink
removed old keygen
Browse files Browse the repository at this point in the history
  • Loading branch information
rlkelly committed May 13, 2018
1 parent c2fcd87 commit 791ae85
Showing 1 changed file with 2 additions and 48 deletions.
50 changes: 2 additions & 48 deletions src/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,45 +69,6 @@ impl GenKeys {
KeyPair::generate_pkcs8(self).unwrap().to_vec()
}

pub fn gen_n_keys(&self, n_keys: i64, tokens_per_user: i64) -> Vec<(Vec<u8>, i64)> {
let users: Vec<_> = (0..n_keys)
.into_iter()
.map(|_| {
let pkcs8 = self.new_key();
(pkcs8, tokens_per_user)
})
.collect();
users
}
}

impl SecureRandom for GenKeys {
fn fill(&self, dest: &mut [u8]) -> Result<(), Unspecified> {
let mut rng = self.generator.borrow_mut();
rng.fill_bytes(dest);
Ok(())
}
}

pub struct GenKeys2 {
// This is necessary because the rng needs to mutate its state to remain
// deterministic, and the fill trait requires an immuatble reference to self
generator: RefCell<ChaChaRng>,
}

impl GenKeys2 {
pub fn new(seed_values: &[u8]) -> GenKeys2 {
let seed: &[u8] = &seed_values[..];
let rng: ChaChaRng = SeedableRng::from_seed(unsafe { mem::transmute(seed) });
GenKeys2 {
generator: RefCell::new(rng),
}
}

pub fn new_key(&self) -> Vec<u8> {
KeyPair::generate_pkcs8(self).unwrap().to_vec()
}

pub fn gen_n_seeds(&self, n_seeds: i64) -> Vec<[u8; 16]> {
let mut rng = self.generator.borrow_mut();

Expand All @@ -126,7 +87,7 @@ impl GenKeys2 {

let users: Vec<_> = keys.into_par_iter()
.map(|seed| {
let new: GenKeys2 = GenKeys2::new(&seed[..]);
let new: GenKeys = GenKeys::new(&seed[..]);
let pkcs8 = KeyPair::generate_pkcs8(&new).unwrap().to_vec();
(pkcs8, tokens_per_user)
})
Expand All @@ -135,7 +96,7 @@ impl GenKeys2 {
}
}

impl SecureRandom for GenKeys2 {
impl SecureRandom for GenKeys {
fn fill(&self, dest: &mut [u8]) -> Result<(), Unspecified> {
let mut rng = self.generator.borrow_mut();
rng.fill_bytes(dest);
Expand All @@ -159,13 +120,6 @@ mod tests {
b.iter(|| rnd.gen_n_keys(1000, 1));
}

#[bench]
fn bench_gen_keys2(b: &mut Bencher) {
let seed: &[_] = &[1, 2, 3, 4];
let rnd = GenKeys2::new(seed);
b.iter(|| rnd.gen_n_keys(1000, 1));
}

#[test]
fn test_new_key_is_redundant() {
let seed: &[_] = &[1, 2, 3, 4];
Expand Down

0 comments on commit 791ae85

Please sign in to comment.