use rskit_errors::AppResult;
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Algorithm {
AesGcm,
ChaCha20Poly1305,
}
impl Algorithm {
pub fn name(&self) -> &'static str {
match self {
Self::AesGcm => "aes-256-gcm",
Self::ChaCha20Poly1305 => "chacha20-poly1305",
}
}
pub(crate) const fn id(self) -> u8 {
match self {
Self::AesGcm => 1,
Self::ChaCha20Poly1305 => 2,
}
}
pub(crate) fn from_id(id: u8) -> Option<Self> {
match id {
1 => Some(Self::AesGcm),
2 => Some(Self::ChaCha20Poly1305),
_ => None,
}
}
}
impl std::fmt::Display for Algorithm {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.name())
}
}
pub trait Encryptor: Send + Sync {
fn encrypt(&self, plaintext: &[u8]) -> AppResult<String>;
fn decrypt(&self, ciphertext: &str) -> AppResult<Vec<u8>>;
fn algorithm(&self) -> Algorithm;
}