PassKey

Struct PassKey 

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

Key and Salt that are used to encrypt/decrypt data.

Implementations§

Source§

impl PassKey

Source

pub fn from_passphrase(passphrase: &[u8]) -> Result<PassKey, KeyDerivationError>

Create a new PassKey with a random Salt.

Note that passphrase memory is not being zeroed after it has been used. Code that provides passphrase should take care of zeroing that memory.

Can fail for the same reasons as PassKey::with_salt() (./struct.PassKey.html#method.with_salt), that is:

  • passphrase is empty
  • deriving key failed (can happen due to OOM)

E.g.

use tox_encryptsave::*;

// fails with an empty passphrase
assert_eq!(PassKey::from_passphrase(&[]), Err(KeyDerivationError::Null));
Source

pub fn with_salt( passphrase: &[u8], salt: Salt, ) -> Result<PassKey, KeyDerivationError>

Create a new PassKey with provided Salt, rather than using a random one.

Note that passphrase memory is not being zeroed after it has been used. Code that provides passphrase should take care of zeroing that memory.

§Fails when:
 * passphrase is empty
 * deriving key failed (can happen due to OOM)

E.g.

use tox_crypto::pwhash::gen_salt;
use tox_encryptsave::*;

assert_eq!(PassKey::with_salt(&[], gen_salt()), Err(KeyDerivationError::Null));
Source

pub fn encrypt(&self, data: &[u8]) -> Result<Vec<u8>, EncryptionError>

Encrypts provided data with self PassKey.

Encrypted data is bigger than supplied data by EXTRA_LENGTH (./constant.EXTRA_LENGTH.html).

§Fails when:
  • provided data is empty

E.g.

use tox_encryptsave::*;

                                      // ↓ don't
let passkey = PassKey::from_passphrase(&[0]).expect("Failed to unwrap PassKey!");

assert_eq!(passkey.encrypt(&[]), Err(EncryptionError::Null));
Source

pub fn decrypt(&self, data: &[u8]) -> Result<Vec<u8>, DecryptionError>

Decrypts provided data with self PassKey.

Decrypted data is smaller by EXTRA_LENGTH than encrypted data.

§Fails when:
  • provided data is empty
  • size of provided data is less than EXTRA_LENGTH
  • format of provided data is wrong
  • decrypting data fails
    • could be due to OOM or by providing bytes that aren’t encrypted after encrypted part

E.g.

use tox_encryptsave::*;

                                      // ↓ don't
let passkey = PassKey::from_passphrase(&[0]).expect("Failed to unwrap PassKey!");

// empty data
assert_eq!(passkey.decrypt(&[]), Err(DecryptionError::Null));

Trait Implementations§

Source§

impl Clone for PassKey

Source§

fn clone(&self) -> PassKey

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 Debug for PassKey

Source§

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

Formats the value using the given formatter. Read more
Source§

impl PartialEq for PassKey

Source§

fn eq(&self, other: &PassKey) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for PassKey

Source§

impl StructuralPartialEq for PassKey

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> 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.