copernica_packets/
nonce.rs

1use {
2  copernica_common::constants::NONCE_SIZE,
3  rand::RngCore,
4};
5#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
6pub struct Nonce(pub [u8; NONCE_SIZE]);
7impl Nonce {
8    pub fn as_bytes(&self) -> Vec<u8> {
9        self.0.to_vec()
10    }
11    pub fn from_bytes(data: &[u8]) -> Self {
12        let mut nonce = [0u8; NONCE_SIZE];
13        nonce.clone_from_slice(&data[..NONCE_SIZE]);
14        Self(nonce)
15    }
16    pub fn new() -> Self {
17        let mut rng = rand::thread_rng();
18        let mut nonce = Nonce([0; NONCE_SIZE]);
19        rng.fill_bytes(&mut nonce.0);
20        nonce
21    }
22}