Update aes dependency
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2022-08-20 09:30:36 +02:00
parent 8b2d3b7fae
commit 68e0aa2565
4 changed files with 114 additions and 17 deletions

View File

@ -1,8 +1,7 @@
use std::io::ErrorKind;
use aes_gcm::{Aes256Gcm, Key, Nonce};
use aes_gcm::aead::Aead;
use aes_gcm::NewAead;
use aes_gcm::{Aes256Gcm, Key, KeyInit, Nonce};
use aes_gcm::aead::{Aead, OsRng};
use rand::Rng;
use serde::de::DeserializeOwned;
use serde::Serialize;
@ -10,21 +9,20 @@ use serde::Serialize;
use crate::utils::err::Res;
const NONCE_LEN: usize = 12;
const KEY_LEN: usize = 32;
pub struct CryptoWrapper {
key: Vec<u8>,
key: Key<Aes256Gcm>,
}
impl CryptoWrapper {
/// Generate a new memory wrapper
pub fn new_random() -> Self {
Self { key: (0..KEY_LEN).map(|_| { rand::random::<u8>() }).collect() }
Self { key: Aes256Gcm::generate_key(&mut OsRng) }
}
/// Encrypt some data
pub fn encrypt<T: Serialize + DeserializeOwned>(&self, data: &T) -> Res<String> {
let aes_key = Aes256Gcm::new(Key::from_slice(&self.key));
let aes_key = Aes256Gcm::new(&self.key);
let nonce_bytes = rand::thread_rng().gen::<[u8; NONCE_LEN]>();
let serialized_data = bincode::serialize(data)?;
@ -49,7 +47,7 @@ impl CryptoWrapper {
let (enc, nonce) = bytes.split_at(bytes.len() - NONCE_LEN);
assert_eq!(nonce.len(), NONCE_LEN);
let aes_key = Aes256Gcm::new(Key::from_slice(&self.key));
let aes_key = Aes256Gcm::new(&self.key);
let dec = match aes_key.decrypt(Nonce::from_slice(nonce), enc) {
Ok(d) => d,

View File

@ -30,4 +30,26 @@ pub fn apply_env_vars(val: &str) -> String {
}
val
}
#[cfg(test)]
mod test {
use std::env;
use crate::utils::string_utils::apply_env_vars;
const VAR_ONE: &str = "VAR_ONE";
#[test]
fn test_apply_env_var() {
env::set_var(VAR_ONE, "good");
let src = format!("This is ${{{}}}", VAR_ONE);
assert_eq!("This is good", apply_env_vars(&src));
}
const VAR_INVALID: &str = "VAR_INV@LID";
#[test]
fn test_invalid_var_syntax() {
let src = format!("This is ${{{}}}", VAR_INVALID);
assert_eq!(src, apply_env_vars(&src));
}
}