mod sw_cipher_chirho;
pub use sw_cipher_chirho::{
SwCipherChirho, CipherTypeChirho, SapphireCipherChirho, XorCipherChirho,
};
use crate::error_chirho::ResultChirho;
pub trait CipherChirho: Send + Sync + std::fmt::Debug {
fn encrypt_chirho(&self, data_chirho: &[u8]) -> ResultChirho<Vec<u8>>;
fn decrypt_chirho(&self, data_chirho: &[u8]) -> ResultChirho<Vec<u8>>;
fn name_chirho(&self) -> &str;
fn has_key_chirho(&self) -> bool;
}
pub fn create_cipher_chirho(cipher_type_chirho: CipherTypeChirho, key_chirho: &str) -> Box<dyn CipherChirho> {
match cipher_type_chirho {
CipherTypeChirho::NoneChirho => Box::new(NullCipherChirho),
CipherTypeChirho::SapphireChirho => Box::new(SapphireCipherChirho::new_chirho(key_chirho)),
CipherTypeChirho::XorChirho => Box::new(XorCipherChirho::new_chirho(key_chirho)),
}
}
#[derive(Debug, Clone, Default)]
pub struct NullCipherChirho;
impl CipherChirho for NullCipherChirho {
fn encrypt_chirho(&self, data_chirho: &[u8]) -> ResultChirho<Vec<u8>> {
Ok(data_chirho.to_vec())
}
fn decrypt_chirho(&self, data_chirho: &[u8]) -> ResultChirho<Vec<u8>> {
Ok(data_chirho.to_vec())
}
fn name_chirho(&self) -> &str {
"None"
}
fn has_key_chirho(&self) -> bool {
false
}
}
#[cfg(test)]
mod tests_chirho {
use super::*;
#[test]
fn test_null_cipher_chirho() {
let cipher_chirho = NullCipherChirho;
let data_chirho = b"Hello, World!";
let encrypted_chirho = cipher_chirho.encrypt_chirho(data_chirho).unwrap();
assert_eq!(encrypted_chirho, data_chirho);
let decrypted_chirho = cipher_chirho.decrypt_chirho(&encrypted_chirho).unwrap();
assert_eq!(decrypted_chirho, data_chirho);
}
#[test]
fn test_create_cipher_chirho() {
let cipher_chirho = create_cipher_chirho(CipherTypeChirho::NoneChirho, "");
assert_eq!(cipher_chirho.name_chirho(), "None");
let cipher_chirho = create_cipher_chirho(CipherTypeChirho::SapphireChirho, "test");
assert_eq!(cipher_chirho.name_chirho(), "Sapphire");
let cipher_chirho = create_cipher_chirho(CipherTypeChirho::XorChirho, "test");
assert_eq!(cipher_chirho.name_chirho(), "XOR");
}
}