pub mod age;
pub mod gpg;
pub use age::AGE;
pub use gpg::GPG;
use crate::error::{Error, Result};
#[typetag::serde(tag = "type")]
pub trait EncryptionType {
fn new(key: String) -> Self
where
Self: Sized;
fn set_key(&mut self, key: String);
fn get_key(&self) -> String;
fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>>;
fn decrypt(&self, encrypted_data: &[u8]) -> Result<Vec<u8>>;
fn as_string(&self) -> &'static str;
fn is_this_type(encrypted_data: &[u8]) -> bool
where
Self: Sized;
}
pub fn create_encryption_type(
key: String,
encryption_type_str: &str,
) -> Result<Box<dyn EncryptionType>> {
match encryption_type_str {
"age" => Ok(Box::new(AGE::new(key))),
"gpg" => Ok(Box::new(GPG::new(key))),
_ => Err(Error::InvalidEncryptionType(
encryption_type_str.to_string(),
)),
}
}
pub fn get_encryption_type(encrypted_content: &[u8]) -> Result<Box<dyn EncryptionType>> {
let e_type = if GPG::is_this_type(encrypted_content) {
"gpg"
} else {
"age"
};
create_encryption_type("".to_string(), e_type)
}