pub struct EncryptionKey { /* private fields */ }Expand description
Encryption key with secure memory handling.
The key bytes are wrapped in Zeroizing to ensure they are
zeroed from memory when dropped.
Implementations§
Source§impl EncryptionKey
impl EncryptionKey
Sourcepub fn derive_from_password(password: &str, salt: &[u8]) -> Result<Self>
pub fn derive_from_password(password: &str, salt: &[u8]) -> Result<Self>
Sourcepub fn generate() -> Self
pub fn generate() -> Self
Generates a random 32-byte encryption key.
Uses the operating system’s cryptographically secure random number generator.
§Panics
Panics if the OS random number generator fails.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self>
pub fn from_bytes(bytes: &[u8]) -> Result<Self>
Sourcepub fn as_bytes(&self) -> &[u8] ⓘ
pub fn as_bytes(&self) -> &[u8] ⓘ
Returns the raw key bytes.
Use with caution - only for persisting the key securely.
Sourcepub fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>>
pub fn encrypt(&self, plaintext: &[u8]) -> Result<Vec<u8>>
Encrypts plaintext using XChaCha20-Poly1305.
The returned ciphertext has the 24-byte nonce prepended:
[nonce (24 bytes)][ciphertext + auth tag]
§Arguments
plaintext- The data to encrypt
§Errors
Returns SecretsError::Encryption if encryption fails.
§Panics
Panics if the OS random number generator fails to produce nonce bytes.
Sourcepub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>>
pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>>
Decrypts data that was encrypted with Self::encrypt.
Expects the input format: [nonce (24 bytes)][ciphertext + auth tag]
§Arguments
data- The encrypted data with prepended nonce
§Errors
Returns SecretsError::Decryption if:
- The data is too short (less than nonce size)
- Decryption or authentication fails
Trait Implementations§
Source§impl Clone for EncryptionKey
impl Clone for EncryptionKey
Source§fn clone(&self) -> EncryptionKey
fn clone(&self) -> EncryptionKey
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for EncryptionKey
impl RefUnwindSafe for EncryptionKey
impl Send for EncryptionKey
impl Sync for EncryptionKey
impl Unpin for EncryptionKey
impl UnsafeUnpin for EncryptionKey
impl UnwindSafe for EncryptionKey
Blanket Implementations§
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
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 more