| 1 | //! Randomness from the operating system, for ids and tokens. |
| 2 | |
| 3 | use std::io::Read; |
| 4 | |
| 5 | pub fn bytes(n: usize) -> Vec<u8> { |
| 6 | let mut buf = vec![0u8; n]; |
| 7 | std::fs::File::open("/dev/urandom") |
| 8 | .and_then(|mut f| f.read_exact(&mut buf)) |
| 9 | .expect("the operating system's random source must be readable"); |
| 10 | buf |
| 11 | } |
| 12 | |
| 13 | /// `n` random bytes as hex. |
| 14 | pub fn hex(n: usize) -> String { |
| 15 | crate::hex::encode(&bytes(n)) |
| 16 | } |