This commit is contained in:
@ -8,8 +8,11 @@ use base64::Engine as _;
|
||||
pub use bincode::{Decode, Encode};
|
||||
use rand::Rng;
|
||||
|
||||
/// The lenght of the nonce used to initialize encryption
|
||||
const NONCE_LEN: usize = 12;
|
||||
|
||||
/// CryptoWrapper is a library that can be used to encrypt and decrypt some data marked
|
||||
/// that derives [Encode] and [Decode] traits using AES encryption
|
||||
pub struct CryptoWrapper {
|
||||
key: Key<Aes256Gcm>,
|
||||
}
|
||||
@ -22,7 +25,7 @@ impl CryptoWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
/// Encrypt some data
|
||||
/// Encrypt some data, returning the result as a base64-encoded string
|
||||
pub fn encrypt<T: Encode + Decode>(&self, data: &T) -> Result<String, Box<dyn Error>> {
|
||||
let aes_key = Aes256Gcm::new(&self.key);
|
||||
let nonce_bytes = rand::thread_rng().gen::<[u8; NONCE_LEN]>();
|
||||
|
Reference in New Issue
Block a user