Skip to main content

Encrypter

Struct Encrypter 

Source
pub struct Encrypter { /* private fields */ }
Expand description

Encrypts and decrypts with the application key.

Cheap to clone and safe to share, so it is normally registered once as application state and reached with req.state::<Encrypter>().

Implementations§

Source§

impl Encrypter

Source

pub fn new(key: AppKey) -> Self

Build from an application key. Encryption uses its own derived sub-key, never the raw APP_KEY, so a ciphertext and a URL signature can never be confused for one another.

Source

pub fn from_config(config: &Config) -> Result<Self>

Build from app.key in the configuration tree.

Source

pub fn generate_key() -> String

A fresh APP_KEY line for .env, used by rustlavel key:generate.

Source

pub fn encrypt(&self, plaintext: &str) -> Result<String>

Encrypt a string.

Source

pub fn encrypt_bytes(&self, plaintext: &[u8]) -> Result<String>

Encrypt arbitrary bytes.

A fresh nonce is drawn for every message. Reusing a nonce under one key is the single fatal mistake in GCM — it leaks the XOR of two plaintexts and, worse, the authentication subkey — so the nonce is never derived from the message, a counter, or the clock.

Source

pub fn decrypt(&self, payload: &str) -> Result<String>

Decrypt a payload back into a string.

Source

pub fn decrypt_bytes(&self, payload: &str) -> Result<Vec<u8>>

Decrypt a payload back into bytes.

Every failure — wrong key, flipped bit, truncated string, unknown version — returns the same kind of error and reveals nothing about which one it was. A caller that told an attacker “the tag was wrong” rather than “the base64 was wrong” would be handing out an oracle.

Trait Implementations§

Source§

impl Clone for Encrypter

Source§

fn clone(&self) -> Encrypter

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Encrypter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.