From 977323ccb5c219d486adbc36592ed8d5cefb2155 Mon Sep 17 00:00:00 2001 From: Demur Rumed Date: Tue, 28 Feb 2017 14:49:41 +0000 Subject: [PATCH] Replace time with std::time --- Cargo.toml | 1 - src/fortuna.rs | 20 ++++++++++++-------- src/lib.rs | 1 - 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 8ea941a7..4fd3286b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,6 +21,5 @@ gcc = "^0.3" [dependencies] libc = "^0.2" -time = "^0.1" rand = "^0.3" rustc-serialize = "^0.3" diff --git a/src/fortuna.rs b/src/fortuna.rs index 0781f317..f074cb96 100644 --- a/src/fortuna.rs +++ b/src/fortuna.rs @@ -46,8 +46,8 @@ use cryptoutil::copy_memory; +use std::time::{Duration, Instant}; use rand::{Rng, SeedableRng}; -use time::precise_time_s; use aessafe::AesSafe256Encryptor; use cryptoutil::read_u32_le; @@ -180,7 +180,7 @@ pub struct Fortuna { pool: [Pool; NUM_POOLS], generator: FortunaGenerator, reseed_count: u32, - last_reseed_time: f64 + last_reseed_time: Option } impl Fortuna { @@ -190,7 +190,7 @@ impl Fortuna { pool: [Pool::new(); NUM_POOLS], generator: FortunaGenerator::new(), reseed_count: 0, - last_reseed_time: 0.0 + last_reseed_time: None, } } @@ -216,11 +216,15 @@ impl Rng for Fortuna { /// pool, this function will fail the task. fn fill_bytes(&mut self, dest: &mut [u8]) { // Reseed if necessary - let now = precise_time_s(); if self.pool[0].count >= MIN_POOL_SIZE && - now - self.last_reseed_time > 0.1 { + if let Some(last_reseed_time) = self.last_reseed_time { + last_reseed_time.elapsed() > Duration::from_millis(100) + } else { + true + } + { self.reseed_count += 1; - self.last_reseed_time = now; + self.last_reseed_time = Some(Instant::now()); // Compute key as Sha256d( key || s ) let mut hash = [0; (32 * NUM_POOLS)]; let mut n_pools = 0; @@ -259,14 +263,14 @@ impl<'a> SeedableRng<&'a [u8]> for Fortuna { fn reseed(&mut self, seed: &'a [u8]) { self.reseed_count += 1; - self.last_reseed_time = precise_time_s(); + self.last_reseed_time = Some(Instant::now()); self.generator.reseed(seed); } } #[cfg(test)] fn test_force_reseed(f: &mut Fortuna) { - f.last_reseed_time -= 0.2; + f.last_reseed_time = Some(Instant::now() - Duration::from_millis(200)); } #[cfg(test)] diff --git a/src/lib.rs b/src/lib.rs index 59ad3afb..27a230a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,7 +8,6 @@ extern crate rand; extern crate rustc_serialize as serialize; -extern crate time; extern crate libc; #[cfg(all(test, feature = "with-bench"))]