use std::fmt;
use std::io::{self, Read, Write};
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Error {
Encrypt,
Decrypt,
Algorithm,
Base64Decode(String),
Read(String),
Write(String),
Platform(String),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Encrypt => write!(f, "Could not encrypt input."),
Self::Decrypt => write!(
f,
"\
Could not decrypt input.
You are likely using the wrong key, or the data is corrupted."
),
Self::Algorithm => write!(f, "Incompatible cipher algorithm."),
Self::Base64Decode(reason) => write!(f, "Could not decode base64: {reason}"),
Self::Read(reason) => write!(f, "Could not read from input: {reason}"),
Self::Write(reason) => write!(f, "Could not write to output: {reason}"),
Self::Platform(reason) => write!(f, "{reason}"),
}
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub trait Cipher {
#[must_use]
fn generate_key() -> Vec<u8>;
fn encrypt(key: &[u8], plaintext: &[u8]) -> Result<Vec<u8>> {
let mut encrypted = Vec::new();
Self::encrypt_stream(key, &mut io::Cursor::new(plaintext), &mut encrypted)?;
Ok(encrypted)
}
fn decrypt(key: &[u8], ciphertext: &[u8]) -> Result<Vec<u8>> {
let mut decrypted = Vec::new();
Self::decrypt_stream(key, &mut io::Cursor::new(ciphertext), &mut decrypted)?;
Ok(decrypted)
}
fn encrypt_stream<R: Read, W: Write>(key: &[u8], reader: &mut R, writer: &mut W) -> Result<()>;
fn decrypt_stream<R: Read, W: Write>(key: &[u8], reader: &mut R, writer: &mut W) -> Result<()>;
}
pub trait Base64Encode {
#[must_use]
fn base64_encode(&self) -> String;
}
pub trait Base64Decode {
fn base64_decode(&self) -> Result<Vec<u8>>;
}