pub struct FieldEncryption { /* private fields */ }Expand description
Cipher for field-level encryption using AES-256-GCM
Encrypts sensitive database fields with authenticated encryption. Each encryption uses a random nonce, preventing identical plaintexts from producing identical ciphertexts.
§Example
let cipher = FieldEncryption::new("encryption-key".as_bytes());
let encrypted = cipher.encrypt("user@example.com")?;
let decrypted = cipher.decrypt(&encrypted)?;
assert_eq!(decrypted, "user@example.com");Implementations§
Source§impl FieldEncryption
impl FieldEncryption
Sourcepub fn encrypt(&self, plaintext: &str) -> Result<Vec<u8>, SecretsError>
pub fn encrypt(&self, plaintext: &str) -> Result<Vec<u8>, SecretsError>
Encrypt plaintext field using AES-256-GCM
Generates random 96-bit nonce, encrypts with authenticated encryption, and returns [nonce || ciphertext] format for decryption.
§Arguments
plaintext- Data to encrypt
§Returns
Encrypted data in format: [12-byte nonce][ciphertext + 16-byte tag]
§Errors
Returns EncryptionError if encryption fails
Sourcepub fn decrypt(&self, encrypted: &[u8]) -> Result<String, SecretsError>
pub fn decrypt(&self, encrypted: &[u8]) -> Result<String, SecretsError>
Decrypt encrypted field using AES-256-GCM
Expects data in format: [12-byte nonce][ciphertext + 16-byte tag] Extracts nonce, decrypts, and verifies authentication tag.
§Arguments
encrypted- Encrypted data from encrypt()
§Returns
Decrypted plaintext as String
§Errors
Returns EncryptionError if:
- Data too short for nonce
- Decryption fails (wrong key or corrupted data)
- Plaintext is not valid UTF-8
Sourcepub fn encrypt_with_context(
&self,
plaintext: &str,
context: &str,
) -> Result<Vec<u8>, SecretsError>
pub fn encrypt_with_context( &self, plaintext: &str, context: &str, ) -> Result<Vec<u8>, SecretsError>
Encrypt field with additional context for audit/security
Includes context (e.g., user_id, field_name) in authenticated data but not in ciphertext, providing audit trail without bloating storage.
§Arguments
plaintext- Data to encryptcontext- Additional authenticated data (e.g., “user:123:email”)
§Returns
Encrypted data in format: [12-byte nonce][ciphertext + 16-byte tag]
Sourcepub fn decrypt_with_context(
&self,
encrypted: &[u8],
context: &str,
) -> Result<String, SecretsError>
pub fn decrypt_with_context( &self, encrypted: &[u8], context: &str, ) -> Result<String, SecretsError>
Decrypt field with additional context verification
Context must match the value used during encryption for verification to succeed.
§Arguments
encrypted- Encrypted data from encrypt_with_context()context- Context that was used during encryption
§Returns
Decrypted plaintext as String
§Errors
Returns EncryptionError if context doesn’t match or decryption fails
Trait Implementations§
Source§impl Clone for FieldEncryption
impl Clone for FieldEncryption
Source§fn clone(&self) -> FieldEncryption
fn clone(&self) -> FieldEncryption
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for FieldEncryption
impl RefUnwindSafe for FieldEncryption
impl Send for FieldEncryption
impl Sync for FieldEncryption
impl Unpin for FieldEncryption
impl UnwindSafe for FieldEncryption
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request