pub struct Encryptor { /* private fields */ }Expand description
An encryptor utilizing XChaCha20Poly1305 authenticated encryption
This struct provides an interface for encrypting and decrypting data using a 32-byte symmetric key. It uses XChaCha20Poly1305 for authenticated encryption with associated data (AEAD). The key is wiped from memory on drop.
Derive the key with a KDF (as MessageSession does) rather than using a raw
KEM shared secret, and never let two parties encrypt under the same key.
Implementations§
Source§impl Encryptor
impl Encryptor
Sourcepub fn encrypt(
&self,
plaintext: &[u8],
nonce: &[u8; 24],
) -> Result<Vec<u8>, CryptoError>
pub fn encrypt( &self, plaintext: &[u8], nonce: &[u8; 24], ) -> Result<Vec<u8>, CryptoError>
Encrypts plaintext using XChaCha20Poly1305 with the stored key
§Arguments
plaintext- The data to encryptnonce- A 24-byte nonce (must be unique for each encryption with the same key)
§Returns
Result<Vec<u8>, CryptoError>: The encrypted ciphertext or an error
§Security Notes
- The nonce must never be reused with the same key
- The ciphertext includes an authentication tag to verify integrity
Sourcepub fn decrypt(
&self,
ciphertext: &[u8],
nonce: &[u8; 24],
) -> Result<Vec<u8>, CryptoError>
pub fn decrypt( &self, ciphertext: &[u8], nonce: &[u8; 24], ) -> Result<Vec<u8>, CryptoError>
Decrypts ciphertext using XChaCha20Poly1305 with the stored key
§Arguments
ciphertext- The encrypted data to decryptnonce- The 24-byte nonce used during encryption
§Returns
Result<Vec<u8>, CryptoError>: The decrypted plaintext or an error
§Security Notes
- This function will return an error if the ciphertext has been tampered with
- The same nonce used for encryption must be provided for decryption
Auto Trait Implementations§
impl Freeze for Encryptor
impl RefUnwindSafe for Encryptor
impl Send for Encryptor
impl Sync for Encryptor
impl Unpin for Encryptor
impl UnsafeUnpin for Encryptor
impl UnwindSafe for Encryptor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more