pub struct Crypt { /* private fields */ }Expand description
AES-256-GCM encrypter. The first key encrypts; every key is tried on decrypt,
so rotating APP_KEY keeps old payloads readable via APP_PREVIOUS_KEYS.
Implementations§
Source§impl Crypt
impl Crypt
Sourcepub fn new(key: &str) -> Result<Self, CryptError>
pub fn new(key: &str) -> Result<Self, CryptError>
key is base64:<32 bytes>.
Sourcepub fn with_previous(key: &str, previous: &[&str]) -> Result<Self, CryptError>
pub fn with_previous(key: &str, previous: &[&str]) -> Result<Self, CryptError>
As Crypt::new, plus older keys accepted for decryption only.
Sourcepub fn from_env() -> Result<Self, CryptError>
pub fn from_env() -> Result<Self, CryptError>
Reads APP_KEY and the comma-separated APP_PREVIOUS_KEYS (optional).
Sourcepub fn generate_key() -> String
pub fn generate_key() -> String
A fresh random key in APP_KEY format.
Sourcepub fn encrypt<T: Serialize>(&self, value: &T) -> Result<String, CryptError>
pub fn encrypt<T: Serialize>(&self, value: &T) -> Result<String, CryptError>
Serialize to JSON, encrypt with a random nonce, return url-safe base64
(nonce || ciphertext || tag).
Sourcepub fn decrypt<T: DeserializeOwned>(
&self,
payload: &str,
) -> Result<T, CryptError>
pub fn decrypt<T: DeserializeOwned>( &self, payload: &str, ) -> Result<T, CryptError>
Reverse of Crypt::encrypt. Any tampering, truncation or key mismatch is
a CryptError::Decrypt.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Crypt
impl RefUnwindSafe for Crypt
impl Send for Crypt
impl Sync for Crypt
impl Unpin for Crypt
impl UnsafeUnpin for Crypt
impl UnwindSafe for Crypt
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
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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> ⓘ
Converts
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> ⓘ
Converts
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