use std::error::Error; use aes_gcm::{Aes256Gcm, KeyInit, Nonce}; use aes_gcm::aead::{Aead, Payload}; use rand::Rng; use trifid_pki::rand_core::OsRng; use crate::config::TrifidConfig; pub fn get_cipher_from_config(config: &TrifidConfig) -> Result> { let key_slice = hex::decode(&config.crypto.data_encryption_key)?; Ok(Aes256Gcm::new_from_slice(&key_slice)?) } pub fn encrypt_with_nonce(plaintext: &[u8], nonce: [u8; 12], cipher: &Aes256Gcm) -> Result, aes_gcm::Error> { let nonce = Nonce::from_slice(&nonce); let ciphertext = cipher.encrypt(nonce, plaintext)?; Ok(ciphertext) } pub fn decrypt_with_nonce(ciphertext: &[u8], nonce: [u8; 12], cipher: &Aes256Gcm) -> Result, aes_gcm::Error> { let nonce = Nonce::from_slice(&nonce); let plaintext = cipher.decrypt(nonce, Payload::from(ciphertext))?; Ok(plaintext) } pub fn generate_random_iv() -> [u8; 12] { OsRng.gen() }