pub struct VersionedFieldEncryption { /* private fields */ }Expand description
Multi-key cipher that supports decrypting data from rotated-out keys.
Ciphertexts are prefixed with a 2-byte key version number (little-endian) so the correct key can be selected during decryption. New data is always encrypted with the primary key; old ciphertexts encrypted with a secondary/fallback key remain readable until data is migrated.
§Key rotation workflow
- Promote the new key:
VersionedFieldEncryption::new(new_version, new_key_bytes) - Register the old key as a fallback:
.with_fallback(old_version, old_key_bytes) - All new records are encrypted with the primary (new) key.
- Existing records encrypted with the old key are decrypted successfully via the fallback.
- Migrate old records by reading → decrypting → re-encrypting (see
reencrypt_from_fallback). - Once migration is complete, remove the fallback.
Implementations§
Source§impl VersionedFieldEncryption
impl VersionedFieldEncryption
Sourcepub fn new(
primary_version: KeyVersion,
primary_key: &[u8],
) -> Result<Self, SecretsError>
pub fn new( primary_version: KeyVersion, primary_key: &[u8], ) -> Result<Self, SecretsError>
Create with a single primary key.
§Errors
Returns SecretsError::ValidationError if key is not 32 bytes.
Sourcepub fn with_fallback(
self,
version: KeyVersion,
key: &[u8],
) -> Result<Self, SecretsError>
pub fn with_fallback( self, version: KeyVersion, key: &[u8], ) -> Result<Self, SecretsError>
Register an additional key that can be used for decryption only.
§Errors
Returns SecretsError::ValidationError if key is not 32 bytes.
Sourcepub fn encrypt(&self, plaintext: &str) -> Result<Vec<u8>, SecretsError>
pub fn encrypt(&self, plaintext: &str) -> Result<Vec<u8>, SecretsError>
Encrypt plaintext, embedding the primary key version as a 2-byte LE prefix.
§Errors
Returns SecretsError::EncryptionError on failure.
Sourcepub fn extract_version(encrypted: &[u8]) -> Result<KeyVersion, SecretsError>
pub fn extract_version(encrypted: &[u8]) -> Result<KeyVersion, SecretsError>
Extract the key version from an encrypted blob.
§Errors
Returns error if encrypted is too short to contain the version prefix.
Sourcepub fn decrypt(&self, encrypted: &[u8]) -> Result<String, SecretsError>
pub fn decrypt(&self, encrypted: &[u8]) -> Result<String, SecretsError>
Decrypt an encrypted blob by selecting the key matching the embedded version.
§Errors
Returns SecretsError::EncryptionError if:
- The blob is too short to contain the version prefix.
- The version is unknown (not primary and not a registered fallback).
- Decryption fails (wrong key, corrupted data).
Sourcepub fn reencrypt_from_fallback(
&self,
old_ciphertext: &[u8],
) -> Result<Vec<u8>, SecretsError>
pub fn reencrypt_from_fallback( &self, old_ciphertext: &[u8], ) -> Result<Vec<u8>, SecretsError>
Re-encrypt a ciphertext from a fallback key to the current primary key.
Use this during key rotation to migrate old records without exposing the plaintext outside this function.
§Errors
Returns error if decryption or re-encryption fails.
Auto Trait Implementations§
impl Freeze for VersionedFieldEncryption
impl RefUnwindSafe for VersionedFieldEncryption
impl Send for VersionedFieldEncryption
impl Sync for VersionedFieldEncryption
impl Unpin for VersionedFieldEncryption
impl UnsafeUnpin for VersionedFieldEncryption
impl UnwindSafe for VersionedFieldEncryption
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> 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 more