use rand::RngCore;
pub struct CipherGeneration {}
impl CipherGeneration {
pub fn random_iv() -> Vec<u8> {
Self::random_bytes(12)
}
pub fn random_key() -> String {
hex::encode(Self::random_bytes(16))
}
fn random_bytes(length: usize) -> Vec<u8> {
let mut data = vec![0; length];
rand::thread_rng().fill_bytes(&mut data);
data
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_random_iv() {
let first_random_iv = CipherGeneration::random_iv();
let second_random_iv = CipherGeneration::random_iv();
assert_ne!(first_random_iv, second_random_iv);
}
#[test]
fn test_random_key() {
let first_random_key = CipherGeneration::random_key();
let second_random_key = CipherGeneration::random_key();
assert_ne!(first_random_key, second_random_key);
}
#[test]
fn test_random_bytes() {
let first_random_bytes = CipherGeneration::random_bytes(10);
let second_random_bytes = CipherGeneration::random_bytes(10);
assert_ne!(first_random_bytes, second_random_bytes);
}
}