use crate::dek::DEK;
use crate::error::encryption::EncryptionError;
use crate::held_data::encryption::EncryptionStrategy;
use async_trait::async_trait;
use std::sync::Arc;
pub struct CustomStrategy<ED: Send> {
encrypt_fn: Arc<
dyn (Fn(
DEK,
Vec<u8>,
ED
) -> futures::future::BoxFuture<'static, Result<Vec<u8>, EncryptionError>>) +
Send +
Sync
>,
decrypt_fn: Arc<
dyn (Fn(
DEK,
Vec<u8>,
ED
) -> futures::future::BoxFuture<'static, Result<Vec<u8>, EncryptionError>>) +
Send +
Sync
>,
}
impl<ED: Send> CustomStrategy<ED> {
pub fn new<E, D>(encrypt_fn: E, decrypt_fn: D) -> Self
where
E: Fn(
DEK,
Vec<u8>,
ED
) -> futures::future::BoxFuture<'static, Result<Vec<u8>, EncryptionError>> +
Send +
Sync +
'static,
D: Fn(
DEK,
Vec<u8>,
ED
) -> futures::future::BoxFuture<'static, Result<Vec<u8>, EncryptionError>> +
Send +
Sync +
'static
{
CustomStrategy {
encrypt_fn: Arc::new(encrypt_fn),
decrypt_fn: Arc::new(decrypt_fn),
}
}
}
#[derive(Debug, Clone)]
pub struct CustomEncryptionData {
pub metadata: Vec<u8>,
}
#[async_trait]
impl<ED: Send> EncryptionStrategy for CustomStrategy<ED> {
type EncryptionData = ED;
async fn encrypt(
&self,
dek: DEK,
plaintext: Vec<u8>,
encryption_data: Self::EncryptionData
) -> Result<Vec<u8>, EncryptionError> {
(self.encrypt_fn)(dek, plaintext, encryption_data).await
}
async fn decrypt(
&self,
dek: DEK,
ciphertext: Vec<u8>,
encryption_data: Self::EncryptionData
) -> Result<Vec<u8>, EncryptionError> {
(self.decrypt_fn)(dek, ciphertext, encryption_data).await
}
}