Encryption

Struct Encryption 

Source
pub struct Encryption;
Expand description

structure contains serveral methods to handle encryption

Implementations§

Source§

impl Encryption

Source

pub fn encrypt_pbkdf2sha256_aes128cbc( iterations: u32, salt: &[u8; 32], aes_iv: &[u8; 16], password: impl AsRef<[u8]>, plaintext: &[u8], ) -> Result<Vec<u8>>

encrypts the given plaintext with the given values with PBKDF2-SHA256-AES128CBC, defined in PKCS#5. Returns the ciphertext as Vec<u8>.

§Error

if the encryption fails, or the given parameters are false.

Source

pub fn encrypt_pbkdf2sha256_aes256cbc( iterations: u32, salt: &[u8; 32], aes_iv: &[u8; 16], password: impl AsRef<[u8]>, plaintext: &[u8], ) -> Result<Vec<u8>>

Returns the ciphertext as Vec<u8>.

§Error

if the encryption fails, or the given parameters are false.

Source

pub fn decrypt_pbkdf2sha256_aes128cbc( iterations: u32, salt: &[u8; 32], aes_iv: &[u8; 16], password: impl AsRef<[u8]>, ciphertext: &[u8], ) -> Result<Vec<u8>>

decrypts the given ciphertext from the given values with PBKDF2-SHA256-AES128CBC, defined in PKCS#5. Returns the plaintext as Vec<u8>.

§Error

if the decryption fails, or the given parameters are false.

Source

pub fn decrypt_pbkdf2sha256_aes256cbc( iterations: u32, salt: &[u8; 32], aes_iv: &[u8; 16], password: impl AsRef<[u8]>, ciphertext: &[u8], ) -> Result<Vec<u8>>

decrypts the given ciphertext from the given values with PBKDF2-SHA256-AES256CBC, defined in PKCS#5. Returns the plaintext as Vec<u8>.

§Error

if the decryption fails, or the given parameters are false.

Source

pub fn encrypt_scrypt_aes128cbc( logn: u8, r: u32, p: u32, salt: &[u8; 32], aes_iv: &[u8; 16], password: impl AsRef<[u8]>, plaintext: &[u8], ) -> Result<Vec<u8>>

encrypts the given plaintext with the given values with Scrypt-AES128CBC. Returns the ciphertext as Vec<u8>.

§Error

if the encryption fails, or the given parameters are false.

Source

pub fn encrypt_scrypt_aes256cbc( logn: u8, r: u32, p: u32, salt: &[u8; 32], aes_iv: &[u8; 16], password: impl AsRef<[u8]>, plaintext: &[u8], ) -> Result<Vec<u8>>

encrypts the given plaintext with the given values with Scrypt-AES256CBC. Returns the ciphertext as Vec<u8>.

§Error

if the encryption fails, or the given parameters are false.

Source

pub fn decrypt_scrypt_aes128cbc( logn: u8, r: u32, p: u32, salt: &[u8; 32], aes_iv: &[u8; 16], password: impl AsRef<[u8]>, plaintext: &[u8], ) -> Result<Vec<u8>>

decrypts the given ciphertext with the given values with Scrypt-AES128CBC. Returns the plaintext as Vec<u8>.

§Error

if the encryption fails, or the given parameters are false.

Source

pub fn decrypt_scrypt_aes256cbc( logn: u8, r: u32, p: u32, salt: &[u8; 32], aes_iv: &[u8; 16], password: impl AsRef<[u8]>, plaintext: &[u8], ) -> Result<Vec<u8>>

decrypts the given ciphertext with the given values with Scrypt-AES256CBC. Returns the plaintext as Vec<u8>.

§Error

if the encryption fails, or the given parameters are false.

Source

pub fn encrypt_message<K, M, A>( key: K, message: M, chunk_no: u64, algorithm: A, ) -> Result<Vec<u8>>
where K: AsRef<[u8]>, M: AsRef<[u8]>, A: Borrow<EncryptionAlgorithm>,

method to encrypt a message with a key and and the given chunk number. This method should primary used to encrypt the given chunk data (if selected, then after the compression). Returns a the cipthertext as Vec<u8>.

§Example
use zff::*;
use hex::ToHex;

fn main() -> Result<()> {
       let key = "01234567890123456789012345678912"; // 32Byte/256Bit Key
       let chunk_no = 1; // 12Byte/96Bit Key
       let message = "My message";
 
       let ciphertext = Encryption::encrypt_message(key, message, chunk_no, EncryptionAlgorithm::AES256GCMSIV)?;
 
       assert_eq!(ciphertext.encode_hex::<String>(), "32f1c2f8ff6594a07eda5a4eca6d198f4cda8935f171d2345888".to_string());
       Ok(())
}
§Error

This method will fail, if the encryption fails.

Source

pub fn decrypt_message<K, M, A>( key: K, message: M, chunk_no: u64, algorithm: A, ) -> Result<Vec<u8>>
where K: AsRef<[u8]>, M: AsRef<[u8]>, A: Borrow<EncryptionAlgorithm>,

method to decrypt a message with a key and and the given chunk number. This method should primary used to decrypt the given chunk data (if selected, then before the decompression). Returns a the plaintext as Vec<u8> of the given ciphertext.

§Error

This method will fail, if the decryption fails.

Source

pub fn gen_random_key(length: usize) -> Vec<u8>

Generates a new random key, with the given key size.

§Example
use zff::*;
 
let keysize = 256; //(e.g. for use as 256-Bit-AES-Key).
let my_new_random_super_secret_key = Encryption::gen_random_key(keysize);
//...
Source

pub fn gen_random_iv() -> [u8; 16]

Generates a new random IV/Nonce as [u8; 16] for use in PBE header.

Source

pub fn gen_random_salt() -> [u8; 32]

Generates a new random salt as [u8; 32] for use in PBE header.

Source

pub fn gen_random_header_nonce() -> [u8; 12]

Generates a new random IV/Nonce as [u8; 12] for use in encryption header.

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V