redact_crypto/
algorithm.rs1use crate::{
2 key::sodiumoxide::{
3 SodiumOxidePublicAsymmetricKeyAlgorithm, SodiumOxideSecretAsymmetricKeyAlgorithm,
4 SodiumOxideSymmetricKeyAlgorithm,
5 },
6 ByteSource, CryptoError,
7};
8use async_trait::async_trait;
9use serde::{Deserialize, Serialize};
10
11#[async_trait]
12pub trait Algorithm {
13 type Source;
14 type Output;
15
16 async fn unseal(&self, source: &Self::Source) -> Result<Self::Output, CryptoError>;
17 async fn seal(&self, source: &Self::Source) -> Result<Self::Output, CryptoError>;
18}
19
20#[derive(Serialize, Deserialize, Debug)]
21#[serde(tag = "t", content = "c")]
22pub enum ByteAlgorithm {
23 SodiumOxideSymmetricKey(SodiumOxideSymmetricKeyAlgorithm),
24 SodiumOxideSecretAsymmetricKey(SodiumOxideSecretAsymmetricKeyAlgorithm),
25 SodiumOxidePublicAsymmetricKey(SodiumOxidePublicAsymmetricKeyAlgorithm),
26}
27
28#[async_trait]
29impl Algorithm for ByteAlgorithm {
30 type Source = ByteSource;
31 type Output = ByteSource;
32
33 async fn unseal(&self, source: &Self::Source) -> Result<Self::Output, CryptoError> {
34 match self {
35 Self::SodiumOxideSymmetricKey(sosku) => sosku.unseal(source).await,
36 Self::SodiumOxideSecretAsymmetricKey(sosaku) => sosaku.unseal(source).await,
37 Self::SodiumOxidePublicAsymmetricKey(sopaku) => sopaku.unseal(source).await,
38 }
39 }
40
41 async fn seal(&self, source: &Self::Source) -> Result<Self::Output, CryptoError> {
42 match self {
43 Self::SodiumOxideSymmetricKey(sosku) => sosku.seal(source).await,
44 Self::SodiumOxideSecretAsymmetricKey(sosaku) => sosaku.seal(source).await,
45 Self::SodiumOxidePublicAsymmetricKey(sopaku) => sopaku.seal(source).await,
46 }
47 }
48}
49
50