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
impl Encryptor
Sourcepub fn new(password: &str) -> Result<Self>
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.
Sourcepub fn with_iterations(self, iterations: u32) -> Self
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.
Sourcepub fn with_salt_size(self, salt_size: usize) -> Self
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.
Sourcepub fn with_key_size(self, key_size: usize) -> Result<Self>
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.
Sourcepub fn iterations(&self) -> u32
pub fn iterations(&self) -> u32
The configured PBKDF2 iteration count.
Sourcepub fn encrypt(&self, plaintext: &str) -> Result<String>
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.
Sourcepub fn decrypt(&self, encoded: &str) -> Result<String>
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
impl Encryptor
Sourcepub fn encrypt_with_prefix(&self, plaintext: &str) -> Result<String>
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.
Sourcepub fn decrypt_prefixed(&self, value: &str) -> Result<String>
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.
Sourcepub fn decrypt_all_in_string(&self, input: &str) -> Result<String>
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.
Sourcepub fn decrypt_all_in_string_lossy(&self, input: &str) -> String
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.
Sourcepub fn decrypt_map(
&self,
map: &HashMap<String, String>,
) -> Result<HashMap<String, String>>
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 StringEncryptor for Encryptor
impl StringEncryptor for Encryptor
Source§fn encrypt(&self, plaintext: &str) -> Result<String>
fn encrypt(&self, plaintext: &str) -> Result<String>
plaintext and returns the raw base64 payload (no ENC(...)).Source§fn decrypt(&self, encoded: &str) -> Result<String>
fn decrypt(&self, encoded: &str) -> Result<String>
ENC(...) wrapper).Source§fn encrypt_with_prefix(&self, plaintext: &str) -> Result<String>
fn encrypt_with_prefix(&self, plaintext: &str) -> Result<String>
plaintext and wraps the result as ENC(...).Source§fn decrypt_prefixed(&self, value: &str) -> Result<String>
fn decrypt_prefixed(&self, value: &str) -> Result<String>
ENC(...)-wrapped value. Surrounding whitespace is ignored. Read moreSource§fn decrypt_all_in_string_lossy(&self, input: &str) -> String
fn decrypt_all_in_string_lossy(&self, input: &str) -> String
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).