Skip to content

Commit

Permalink
Enable seeding of crypto.getRandomValues()
Browse files Browse the repository at this point in the history
  • Loading branch information
mtharrison committed Jun 11, 2019
1 parent 841565e commit b4706fd
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
10 changes: 8 additions & 2 deletions cli/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct DenoFlags {
pub allow_hrtime: bool,
pub no_prompts: bool,
pub no_fetch: bool,
pub seed: Option<u64>,
pub v8_flags: Option<Vec<String>>,
pub xeval_replvar: Option<String>,
pub xeval_delim: Option<String>,
Expand Down Expand Up @@ -152,7 +153,7 @@ To get help on the another subcommands (run in this case):
.help("Seed Math.random()")
.takes_value(true)
.validator(|val: String| {
match val.parse::<i64>() {
match val.parse::<u64>() {
Ok(_) => Ok(()),
Err(_) => Err("Seed should be a number".to_string())
}
Expand Down Expand Up @@ -393,7 +394,10 @@ pub fn parse_flags(matches: &ArgMatches) -> DenoFlags {
flags.v8_flags = Some(v8_flags);
}
if matches.is_present("seed") {
let seed = matches.value_of("seed").unwrap();
let seed_string = matches.value_of("seed").unwrap();
let seed = seed_string.parse::<u64>().unwrap();
flags.seed = Some(seed);

let v8_seed_flag = format!("--random-seed={}", seed);

match flags.v8_flags {
Expand Down Expand Up @@ -1146,6 +1150,7 @@ mod tests {
assert_eq!(
flags,
DenoFlags {
seed: Some(250 as u64),
v8_flags: Some(svec!["deno", "--random-seed=250"]),
..DenoFlags::default()
}
Expand All @@ -1167,6 +1172,7 @@ mod tests {
assert_eq!(
flags,
DenoFlags {
seed: Some(250 as u64),
v8_flags: Some(svec!["deno", "--expose-gc", "--random-seed=250"]),
..DenoFlags::default()
}
Expand Down
11 changes: 9 additions & 2 deletions cli/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2211,10 +2211,17 @@ fn op_host_post_message(
}

fn op_get_random_values(
_state: &ThreadSafeState,
state: &ThreadSafeState,
_base: &msg::Base<'_>,
data: Option<PinnedBuf>,
) -> Box<OpWithError> {
thread_rng().fill(&mut data.unwrap()[..]);
if let Some(ref seeded_rng) = state.seeded_rng {
let mut rng = seeded_rng.lock().unwrap();
rng.fill(&mut data.unwrap()[..]);
} else {
let mut rng = thread_rng();
rng.fill(&mut data.unwrap()[..]);
}

Box::new(ok_future(empty_buf()))
}
9 changes: 9 additions & 0 deletions cli/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ use deno::PinnedBuf;
use futures::future::Either;
use futures::future::Shared;
use futures::Future;
use rand::rngs::StdRng;
use rand::SeedableRng;
use std;
use std::collections::HashMap;
use std::collections::HashSet;
Expand Down Expand Up @@ -82,6 +84,7 @@ pub struct State {
pub dispatch_selector: ops::OpSelector,
/// Reference to global progress bar.
pub progress: Progress,
pub seeded_rng: Option<Mutex<StdRng>>,

/// Set of all URLs that have been compiled. This is a hacky way to work
/// around the fact that --reload will force multiple compilations of the same
Expand Down Expand Up @@ -295,6 +298,11 @@ impl ThreadSafeState {
}
}

let mut seeded_rng = None;
if let Some(seed) = flags.seed {
seeded_rng = Some(Mutex::new(StdRng::seed_from_u64(seed)));
};

ThreadSafeState(Arc::new(State {
main_module,
dir,
Expand All @@ -312,6 +320,7 @@ impl ThreadSafeState {
resource,
dispatch_selector,
progress,
seeded_rng,
compiled: Mutex::new(HashSet::new()),
}))
}
Expand Down
8 changes: 8 additions & 0 deletions tests/seed_random.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
for (let i = 0; i < 10; ++i) {
console.log(Math.random());
}

const arr = new Uint8Array(32);

crypto.getRandomValues(arr);
console.log(arr);

crypto.getRandomValues(arr);
console.log(arr);
2 changes: 2 additions & 0 deletions tests/seed_random.js.out
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@
0.3824611207183364
0.5950178237266042
0.22440633214343908
Uint8Array [ 31, 147, 233, 143, 64, 159, 189, 114, 137, 153, 196, 156, 133, 210, 78, 4, 125, 255, 147, 234, 169, 149, 228, 46, 166, 246, 137, 49, 50, 182, 106, 219 ]
Uint8Array [ 220, 209, 104, 94, 239, 165, 8, 254, 123, 163, 160, 177, 229, 105, 171, 232, 236, 71, 107, 28, 132, 143, 113, 44, 86, 251, 159, 102, 20, 119, 174, 230 ]

0 comments on commit b4706fd

Please sign in to comment.