Skip to main content

StringEncryptor

Trait StringEncryptor 

Source
pub trait StringEncryptor {
    // Required methods
    fn encrypt(&self, plaintext: &str) -> Result<String>;
    fn decrypt(&self, encoded: &str) -> Result<String>;

    // Provided methods
    fn encrypt_with_prefix(&self, plaintext: &str) -> Result<String> { ... }
    fn decrypt_prefixed(&self, value: &str) -> Result<String> { ... }
    fn decrypt_all_in_string(&self, input: &str) -> Result<String> { ... }
    fn decrypt_all_in_string_lossy(&self, input: &str) -> String { ... }
    fn decrypt_map(
        &self,
        map: &HashMap<String, String>,
    ) -> Result<HashMap<String, String>> { ... }
}
Expand description

Common interface implemented by Encryptor, JasyptEncryptor and JasyptStrongEncryptor.

Every concrete encryptor also exposes these methods inherently, so you only need to import the trait when writing code that is generic over the encryptor (for example fn load<E: StringEncryptor>(enc: &E)), or when working with Box<dyn StringEncryptor>.

Required Methods§

Source

fn encrypt(&self, plaintext: &str) -> Result<String>

Encrypts plaintext and returns the raw base64 payload (no ENC(...)).

Source

fn decrypt(&self, encoded: &str) -> Result<String>

Decrypts a raw base64 payload (no ENC(...) wrapper).

Provided Methods§

Source

fn encrypt_with_prefix(&self, plaintext: &str) -> Result<String>

Encrypts plaintext and wraps the result as ENC(...).

Source

fn decrypt_prefixed(&self, value: &str) -> Result<String>

Decrypts an ENC(...)-wrapped value. Surrounding whitespace is ignored.

Returns Error::InvalidEncFormat when the wrapper is missing.

Source

fn decrypt_all_in_string(&self, input: &str) -> Result<String>

Replaces every ENC(...) occurrence in input with its plaintext.

Matches that fail to decrypt are left untouched and, after the whole input has been processed, the last error is returned (GoCrypt semantics). Use decrypt_all_in_string_lossy when you want the best-effort output regardless of failures.

Source

fn decrypt_all_in_string_lossy(&self, input: &str) -> String

Like decrypt_all_in_string but never fails: matches that cannot be decrypted are kept verbatim (NodeCrypt/PyCrypt semantics, and what the rustcrypt decrypt-file command uses).

Source

fn decrypt_map( &self, map: &HashMap<String, String>, ) -> Result<HashMap<String, String>>

Decrypts every ENC(...) value in map; other values are copied as-is.

The first failure aborts with Error::KeyDecrypt naming the key.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl<T: StringEncryptor + ?Sized> StringEncryptor for &T

Source§

fn encrypt(&self, plaintext: &str) -> Result<String>

Source§

fn decrypt(&self, encoded: &str) -> Result<String>

Source§

fn encrypt_with_prefix(&self, plaintext: &str) -> Result<String>

Source§

fn decrypt_prefixed(&self, value: &str) -> Result<String>

Source§

fn decrypt_all_in_string(&self, input: &str) -> Result<String>

Source§

fn decrypt_all_in_string_lossy(&self, input: &str) -> String

Source§

fn decrypt_map( &self, map: &HashMap<String, String>, ) -> Result<HashMap<String, String>>

Source§

impl<T: StringEncryptor + ?Sized> StringEncryptor for Box<T>

Source§

fn encrypt(&self, plaintext: &str) -> Result<String>

Source§

fn decrypt(&self, encoded: &str) -> Result<String>

Source§

fn encrypt_with_prefix(&self, plaintext: &str) -> Result<String>

Source§

fn decrypt_prefixed(&self, value: &str) -> Result<String>

Source§

fn decrypt_all_in_string(&self, input: &str) -> Result<String>

Source§

fn decrypt_all_in_string_lossy(&self, input: &str) -> String

Source§

fn decrypt_map( &self, map: &HashMap<String, String>, ) -> Result<HashMap<String, String>>

Implementors§