secure_random/
lib.rs

1/*
2 * Copyright (c) Peter Bjorklund. All rights reserved. https://github.com/nimble-rust/workspace
3 * Licensed under the MIT License. See LICENSE in the project root for license information.
4 */
5use getrandom::getrandom;
6use std::fmt::Debug;
7
8pub trait SecureRandom: Debug {
9    fn get_random_u64(&mut self) -> u64;
10}
11
12#[derive(Debug, Clone)]
13pub struct GetRandom;
14
15impl SecureRandom for GetRandom {
16    fn get_random_u64(&mut self) -> u64 {
17        let mut buf = [0u8; 8];
18        getrandom(&mut buf).expect("failed to get random octets from `getrandom()`");
19        u64::from_le_bytes(buf)
20    }
21}