From 876f25935366bd7c0ec422934b983585e9fec196 Mon Sep 17 00:00:00 2001 From: ddidderr Date: Mon, 12 Feb 2024 09:23:56 +0100 Subject: [PATCH] initial commit --- .gitignore | 1 + Cargo.lock | 16 ++++++++++++++ Cargo.toml | 14 ++++++++++++ rustfmt.toml | 3 +++ src/main.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 rustfmt.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..7050e3c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ez-urandom" +version = "0.1.0" +dependencies = [ + "paste", +] + +[[package]] +name = "paste" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "de3145af08024dea9fa9914f381a17b8fc6034dfb00f3a84013f7ff43f29ed4c" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..df0a261 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "ez-urandom" +version = "0.1.0" +edition = "2021" + +[dependencies] +paste = "1" + +[profile.release] +lto = true +debug = false +strip = true +panic = "unwind" +codegen-units = 1 diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..e270811 --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,3 @@ +group_imports = "StdExternalCrate" +imports_granularity = "Crate" +imports_layout = "HorizontalVertical" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..ccfdd73 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,62 @@ +use std::{ + fs::File, + io::{BufReader, Read}, + mem::{size_of, transmute}, +}; + +use ::paste::paste; + +pub struct OsRandom { + devurandom: BufReader, +} + +impl Default for OsRandom { + fn default() -> Self { + Self::new() + } +} + +macro_rules! os_random_get_integer_impls { + ($($t:ty),*) => { + $( + ::paste::paste! { + pub fn [](&mut self) -> std::io::Result<$t> { + let mut buf = [0u8; size_of::<$t>()]; + self.devurandom.read_exact(&mut buf)?; + Ok(unsafe {transmute::< [u8; size_of::<$t>() ], $t>(buf) }) + } + } + )* + }; +} + +impl OsRandom { + pub fn new() -> Self { + Self { + devurandom: BufReader::new(File::open("/dev/urandom").unwrap()), + } + } + + os_random_get_integer_impls!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); +} + +impl Read for OsRandom { + fn read(&mut self, buf: &mut [u8]) -> std::io::Result { + self.devurandom.read(buf) + } +} + +macro_rules! os_rng_test_impls { + ($($t:ty),*) => { + let mut rng = OsRandom::new(); + $( + paste! { + println!("{}", rng.[]().unwrap()); + } + )* + } +} + +fn main() { + os_rng_test_impls!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize); +}