EncryptionAlgorithm

Trait EncryptionAlgorithm 

Source
pub trait EncryptionAlgorithm: Sealed {
    // Required methods
    fn encrypt_data(data: &[u8], key: &[u8; 32]) -> Result<String, CryptoError>;
    fn decrypt_data(
        encrypted_data: &str,
        key: &[u8; 32],
    ) -> Result<Vec<u8>, CryptoError>;
}
Expand description

Core trait for encryption algorithms used in sentinel-crypto. This trait abstracts encryption operations to allow easy switching between different encryption algorithms while maintaining a consistent interface.

Design choice: Trait-based design enables compile-time algorithm selection and allows for future extensions (e.g., ChaCha20-Poly1305, AES-GCM-SIV) without changing the API. The trait is sealed to prevent external implementations that might not meet security requirements.

Required Methods§

Source

fn encrypt_data(data: &[u8], key: &[u8; 32]) -> Result<String, CryptoError>

Encrypts the given data using the provided key. Returns a hex-encoded string containing nonce + ciphertext.

§Arguments
  • data - The data to encrypt
  • key - The encryption key
§Returns

A hex-encoded string with nonce + ciphertext

§Errors

Returns CryptoError::Encryption if encryption fails

Source

fn decrypt_data( encrypted_data: &str, key: &[u8; 32], ) -> Result<Vec<u8>, CryptoError>

Decrypts the given encrypted data using the provided key. Expects the input to be a hex-encoded string with nonce + ciphertext.

§Arguments
  • encrypted_data - The hex-encoded nonce + ciphertext
  • key - The decryption key
§Returns

The decrypted data

§Errors

Returns CryptoError::Decryption if decryption fails

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§