summary refs log tree commit diff
path: root/rand_ish
diff options
context:
space:
mode:
authorDaniel Prilik <prilik@google.com>2019-01-14 14:19:04 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-01-17 20:20:50 -0800
commit2200604d9c101888df658f9483290a13952c6b1c (patch)
tree8b3982baeeb7f61824f95bd8a91a972f3104b729 /rand_ish
parenta4d5bd4bc271c505097f18ea0506926dbdf8d682 (diff)
downloadcrosvm-2200604d9c101888df658f9483290a13952c6b1c.tar
crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.gz
crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.bz2
crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.lz
crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.xz
crosvm-2200604d9c101888df658f9483290a13952c6b1c.tar.zst
crosvm-2200604d9c101888df658f9483290a13952c6b1c.zip
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 <prilik@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
Diffstat (limited to 'rand_ish')
-rw-r--r--rand_ish/Cargo.toml4
-rw-r--r--rand_ish/src/lib.rs42
2 files changed, 46 insertions, 0 deletions
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<String> {
+    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::<io::Result<String>>()
+}