Skip to main content

Encryptor

Struct Encryptor 

Source
pub struct Encryptor { /* private fields */ }
Expand description

AES-256-GCM encryptor with PBKDF2-HMAC-SHA256 key derivation.

This is the recommended encryptor for values that only need to be read by members of the GoCrypt/PyCrypt/NodeCrypt/PHPCrypt/RustCrypt family. It provides authenticated encryption, so a wrong password or tampered ciphertext is always detected. It is not readable by Java Jasypt; use JasyptEncryptor for that.

§Wire format

base64( salt ‖ nonce ‖ ciphertext ‖ tag ) with a random salt (16 bytes by default), a random 12-byte GCM nonce, and the 16-byte GCM tag appended to the ciphertext. No associated data is used.

§Example

use rustcrypt_jasypt::Encryptor;

let enc = Encryptor::new("my-password")?;
let wrapped = enc.encrypt_with_prefix("db_password_123")?;
assert!(wrapped.starts_with("ENC("));
assert_eq!(enc.decrypt_prefixed(&wrapped)?, "db_password_123");

Parameters can be tuned with the builder methods:

use rustcrypt_jasypt::Encryptor;

let enc = Encryptor::new("my-password")?
    .with_iterations(50_000)
    .with_salt_size(32)
    .with_key_size(32)?;

Implementations§

Source§

impl Encryptor

Source

pub fn new(password: &str) -> Result<Self>

Creates an encryptor with the default parameters (DEFAULT_ITERATIONS, DEFAULT_SALT_SIZE, DEFAULT_KEY_SIZE).

Returns Error::EmptyPassword when password is empty.

Source

pub fn with_iterations(self, iterations: u32) -> Self

Sets the PBKDF2 iteration count (default 10000). Higher is slower but stronger. Both sides must use the same value.

Source

pub fn with_salt_size(self, salt_size: usize) -> Self

Sets the salt size in bytes (default 16). Both sides must use the same value because the salt length is not encoded in the output.

Source

pub fn with_key_size(self, key_size: usize) -> Result<Self>

Sets the AES key size in bytes: 16 (AES-128), 24 (AES-192) or 32 (AES-256, default).

Returns Error::InvalidParameter for any other value.

Source

pub fn iterations(&self) -> u32

The configured PBKDF2 iteration count.

Source

pub fn salt_size(&self) -> usize

The configured salt size in bytes.

Source

pub fn key_size(&self) -> usize

The configured AES key size in bytes.

Source

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

Encrypts plaintext and returns the base64 payload (without the ENC(...) wrapper). A fresh random salt and nonce are used every time.

Returns Error::EmptyValue when plaintext is empty.

Source

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

Decrypts a base64 payload produced by encrypt (or by any family member’s default encryptor with the same parameters).

Returns Error::EmptyValue for empty input, Error::Base64 for malformed base64 and Error::DecryptionFailed for a wrong password, tampered data or a payload that is too short.

Source§

impl Encryptor

Source

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

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

Each call produces a different ciphertext because the salt (and nonce, where applicable) is random.

Source

pub 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

pub 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 for the best-effort output regardless of failures.

Source

pub 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.

Source

pub 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.

Trait Implementations§

Source§

impl Clone for Encryptor

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Encryptor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl StringEncryptor for Encryptor

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).
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. Read more
Source§

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

Replaces every ENC(...) occurrence in input with its plaintext. Read more
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. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.