Created
September 7, 2016 06:34
-
-
Save jamwt/60f66f0fd27d349fe460051e55e79ac9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! Safe cryptographic random number generation in Rust. | |
//! Works on Windows, Mac, Linux, FreeBSD, etc. | |
extern crate rand; // https://crates.io/crates/rand | |
use rand::{Rng}; // The generic trait all random generators support. | |
use rand::os::{OsRng}; // Specific implementation of above for strong crypto. | |
fn main() { | |
// OsRng is a type of `Rng` that wraps /dev/urandom, getrandom(), etc. | |
let mut r = OsRng::new().unwrap(); | |
// Random bytes. | |
let mut my_secure_bytes = vec![0u8; 1500]; | |
r.fill_bytes(&mut my_secure_bytes); | |
// Primitive types and short arrays. | |
let my_secure_int: u64 = r.gen(); | |
println!("First few bytes = {:?}; random int = {:?}", | |
&my_secure_bytes[..5], my_secure_int); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment