Skip to main content

recipher/
key.rs

1use crate::errors::RecipherError;
2use rand::Rng;
3
4pub type Key = [u8; 32];
5pub type Iv = [u8; 16];
6
7pub trait GenRandom {
8    fn gen_random<R: Rng>(rng: &mut R) -> Result<Self, RecipherError>
9    where
10        Self: Sized;
11}
12
13impl GenRandom for Key {
14    fn gen_random<R: Rng>(rng: &mut R) -> Result<Self, RecipherError> {
15        let mut k: Self = Default::default();
16        rng.try_fill_bytes(&mut k)
17            .map_err(|e| RecipherError::RandomizationError(e.to_string()))?;
18
19        Ok(k)
20    }
21}
22
23impl GenRandom for Iv {
24    fn gen_random<R: Rng>(rng: &mut R) -> Result<Self, RecipherError> {
25        let mut iv: Self = Default::default();
26        rng.try_fill_bytes(&mut iv)
27            .map_err(|e| RecipherError::RandomizationError(e.to_string()))?;
28
29        Ok(iv)
30    }
31}