Fernet

Struct Fernet 

Source
pub struct Fernet { /* private fields */ }

Implementations§

Source§

impl Fernet

Fernet encapsulates encrypt and decrypt operations for a particular symmetric key.

Source

pub fn new(key: &str) -> Option<Fernet>

Returns a new fernet instance with the provided key. The key should be 32-bytes, url-safe base64-encoded. Generating keys with Fernet::generate_key is recommended. DO NOT USE A HUMAN READABLE PASSWORD AS A KEY. Returns None if the key is not 32-bytes base64 encoded.

Source

pub fn generate_key() -> String

Generates a new, random, key. Can be safely passed to Fernet::new(). Store this somewhere safe!

Source

pub fn encrypt(&self, data: &[u8]) -> String

Encrypts data into a token. Returns a value (which is base64-encoded) that can be passed to Fernet::decrypt for decryption and verification..

Source

pub fn encrypt_at_time(&self, data: &[u8], current_time: u64) -> String

Encrypts data with the current_time. Returns a value (which is base64-encoded) that can be passed to Fernet::decrypt.

This function has the capacity to be used incorrectly or insecurely due to to the “current_time” parameter. current_time must be the systems time::SystemTime::now() with duraction_since(time::UNIX_EPOCH) as seconds.

The motivation for a function like this is for your application to be able to test ttl expiry of tokens in your API. This allows you to pass in mock time data to assert correct behaviour of your application. Care should be taken to ensure you always pass in correct current_time values for deployments.

Source

pub fn decrypt(&self, token: &str) -> Result<Vec<u8>, DecryptionError>

Decrypts a ciphertext. Returns either Ok(plaintext) if decryption is successful or Err(DecryptionError) if there are any errors. Errors could include incorrect key or tampering with the data.

Source

pub fn decrypt_with_ttl( &self, token: &str, ttl_secs: u64, ) -> Result<Vec<u8>, DecryptionError>

Decrypts a ciphertext with a time-to-live. Returns either Ok(plaintext) if decryption is successful or Err(DecryptionError) if there are any errors. Note if the token timestamp + ttl > current time, then this will also yield a DecryptionError. The ttl is measured in seconds. This is a relative time, not the absolute time of expiry. IE you would use 60 as a ttl_secs if you wanted tokens to be considered invalid after that time.

Source

pub fn decrypt_at_time( &self, token: &str, ttl: Option<u64>, current_time: u64, ) -> Result<Vec<u8>, DecryptionError>

Decrypt a ciphertext with a time-to-live, and the current time. Returns either Ok(plaintext) if decryption is successful or Err(DecryptionError) if there are any errors.

This function has the capacity to be used incorrectly or insecurely due to to the “current_time” parameter. current_time must be the systems time::SystemTime::now() with duraction_since(time::UNIX_EPOCH) as seconds.

The motivation for a function like this is for your application to be able to test ttl expiry of tokens in your API. This allows you to pass in mock time data to assert correct behaviour of your application. Care should be taken to ensure you always pass in correct current_time values for deployments.

Trait Implementations§

Source§

impl Clone for Fernet

Source§

fn clone(&self) -> Fernet

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Zeroize for Fernet

Source§

fn zeroize(&mut self)

Zero out this object from memory using Rust intrinsics which ensure the zeroization operation is not “optimized away” by the compiler.

Auto Trait Implementations§

§

impl Freeze for Fernet

§

impl RefUnwindSafe for Fernet

§

impl Send for Fernet

§

impl Sync for Fernet

§

impl Unpin for Fernet

§

impl UnwindSafe for Fernet

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.