Chacha

Struct Chacha 

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

A tool to encrypt and decrypt data with chacha20poly1305, with a key that is derived from the given password using pbkdf2.

The methods encrypt and decrypt just deal with the secret, the methods encrypt_auth and decrypt_auth use additionally an authentication tag.

The return value of the encrypt methods is of type Cipher and needs to be provided with the same content to the decrypt call.

§Example with authentication tag

use pwsec::Chacha;
let secret = b"this is some serialized form of the secret data";
let auth_tag = b"this is just some informal and nonconfidential summary";
let pw = "LOIUo98zkjhB";
let chacha = Chacha::with_pbkdf2_rounds(123_456);
let cipher = chacha.encrypt_auth(secret, auth_tag, pw).unwrap();

// Decryption needs the cipher, the auth_tag, and the password:
let decrypted_secret = chacha.decrypt_auth(&cipher, auth_tag, pw).unwrap();
assert_eq!(secret.to_vec(), decrypted_secret);

Implementations§

Source§

impl Chacha

Source

pub fn with_pbkdf2_rounds(pbkdf_rounds: u32) -> Self

The constructor takes the number of rounds that pbkdf2 should use.

Choose a random big value, like 125_642 or 101_864, and use the same number for encryption and decryption.

Source

pub fn encrypt(&self, secret: &[u8], pw: &str) -> Result<Cipher, Error>

Encrypts a secret securely, based on a password.

§Parameters:
  • Input:
    • secret: the sensitive data you want to encrypt
    • pw: the password
  • Output:
    • the encrypted secret
§Errors

Error::Encrypt can occur.

Source

pub fn encrypt_auth( &self, secret: &[u8], auth: &[u8], pw: &str, ) -> Result<Cipher, Error>

Encrypts a secret securely, based on a password, and using an authentication tag.

§Parameters:
  • Input:
    • secret: the sensitive data you want to encrypt
    • auth: optional additional data that will not be encrypted, but must be provided in unmodified form also to decrypt_auth
    • pw: the password
  • Output:
    • the sensitive data you had encrypted
§Errors

Error::Encrypt can occur.

Source

pub fn decrypt(&self, cipher: &Cipher, pw: &str) -> Result<Vec<u8>, Error>

Decrypts data that were encrypted with encrypt().

§Parameters:
  • Input:
    • cipher: the output from encrypt
    • auth: the same value as it was given to the encrypt call
    • pw: the password
  • Output:
    • the sensitive data you had encrypted
§Errors

Error::Decrypt can occur.

Source

pub fn decrypt_auth( &self, cipher: &Cipher, auth: &[u8], pw: &str, ) -> Result<Vec<u8>, Error>

Decrypts data that were encrypted with encrypt().

§Parameters:
  • Input:
    • cipher: the output from encrypt
    • auth: the same value as it was given to the encrypt call
    • pw: the password
  • Output:
    • the sensitive data you had encrypted
§Errors

Error::Decrypt can occur.

Auto Trait Implementations§

§

impl Freeze for Chacha

§

impl RefUnwindSafe for Chacha

§

impl Send for Chacha

§

impl Sync for Chacha

§

impl Unpin for Chacha

§

impl UnwindSafe for Chacha

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