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
impl Encrypter
Sourcepub fn new(key: AppKey) -> Self
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.
Sourcepub fn from_config(config: &Config) -> Result<Self>
pub fn from_config(config: &Config) -> Result<Self>
Build from app.key in the configuration tree.
Sourcepub fn generate_key() -> String
pub fn generate_key() -> String
A fresh APP_KEY line for .env, used by rustlavel key:generate.
Sourcepub fn encrypt_bytes(&self, plaintext: &[u8]) -> Result<String>
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.
Sourcepub fn decrypt_bytes(&self, payload: &str) -> Result<Vec<u8>>
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.