From 2200604d9c101888df658f9483290a13952c6b1c Mon Sep 17 00:00:00 2001 From: Daniel Prilik Date: Mon, 14 Jan 2019 14:19:04 -0800 Subject: remove rand crate the few uses of rand::thread_rng() have been replaced with either prngs or reads from /dev/urandom. the implementations are under the `rand_ish` minicrate. `protoc-rust` depends on `tempdir`, which relies on rand, so `tempdir` has been patched with a rewritten version that does not have rand as a dependency. BUG=chromium:921795 TEST=cargo test --features plugin Change-Id: I6f1c7d7a1aeef4dd55ac71e58294d16c291b8871 Reviewed-on: https://chromium-review.googlesource.com/1409705 Commit-Ready: Daniel Prilik Tested-by: kokoro Reviewed-by: Zach Reizner --- rand_ish/Cargo.toml | 4 ++++ rand_ish/src/lib.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 rand_ish/Cargo.toml create mode 100644 rand_ish/src/lib.rs (limited to 'rand_ish') diff --git a/rand_ish/Cargo.toml b/rand_ish/Cargo.toml new file mode 100644 index 0000000..031b97e --- /dev/null +++ b/rand_ish/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "rand_ish" +version = "0.1.0" +authors = ["The Chromium OS Authors"] diff --git a/rand_ish/src/lib.rs b/rand_ish/src/lib.rs new file mode 100644 index 0000000..4372f97 --- /dev/null +++ b/rand_ish/src/lib.rs @@ -0,0 +1,42 @@ +// Copyright 2019 The Chromium OS Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +use std::fs::File; +use std::io::{self, Read}; + +/// A simple prng based on a Linear congruential generator +/// https://en.wikipedia.org/wiki/Linear_congruential_generator +pub struct SimpleRng { + seed: u64, +} + +impl SimpleRng { + /// Create a new SimpleRng + pub fn new(seed: u64) -> SimpleRng { + SimpleRng { seed } + } + + /// Generate random u64 + pub fn rng(&mut self) -> u64 { + // a simple Linear congruential generator + let a: u64 = 6364136223846793005; + let c: u64 = 1442695040888963407; + self.seed = a.wrapping_mul(self.seed).wrapping_add(c); + self.seed + } +} + +/// Samples `/dev/urandom` and generates a random ASCII string of length `len` +pub fn urandom_str(len: usize) -> io::Result { + const ASCII_CHARS: &'static [u8] = b" + ABCDEFGHIJKLMNOPQRSTUVWXYZ\ + abcdefghijklmnopqrstuvwxyz\ + 0123456789"; + + File::open("/dev/urandom")? + .bytes() + .map(|b| b.map(|b| ASCII_CHARS[b as usize % ASCII_CHARS.len()] as char)) + .take(len) + .collect::>() +} -- cgit 1.4.1