Struct Crypto

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

A struct for encrypting/decrypting bytes or io streams.

Implementations§

Source§

impl Crypto

Source

pub fn new<K>(key: K) -> Result<Crypto, ErrorKind>
where K: AsRef<[u8]>,

Create a new Crypto instance, the given key will be used for every operation performed.

Source

pub fn encrypt_with_iv( &self, plaintext: &[u8], iv: &[u8], ) -> Result<Vec<u8>, ErrorKind>

Encrypt bytes with initialisation vector.

Source

pub fn decrypt_with_iv( &self, ciphertext: &[u8], iv: &[u8], ) -> Result<Vec<u8>, ErrorKind>

Decrypt bytes with initialisation vector.

Source

pub fn encrypt<P>(&self, plain: P) -> Result<Vec<u8>, ErrorKind>
where P: AsRef<[u8]>,

Encrypt a small piece of data.

Example:

use fencryption_lib::crypto::Crypto;

let my_super_key = "this_is_super_secure".as_bytes();
let my_super_secret_message = "hello :)".as_bytes();

let crypto = Crypto::new(my_super_key).unwrap();

let enc = crypto.encrypt(my_super_secret_message).unwrap();

assert_ne!(my_super_secret_message, enc);
Source

pub fn decrypt<E>(&self, enc: E) -> Result<Vec<u8>, ErrorKind>
where E: AsRef<[u8]>,

Decrypt a small piece of data.

Example:

use fencryption_lib::crypto::Crypto;

let my_super_key = "this_is_super_secure".as_bytes();
let my_super_secret_message = "hello :)".as_bytes();

let crypto = Crypto::new(my_super_key).unwrap();

let enc = crypto.encrypt(my_super_secret_message).unwrap();
let dec = crypto.decrypt(&enc).unwrap();

assert_eq!(my_super_secret_message, dec);
Source

pub fn encrypt_io( &self, source: &mut impl Read, dest: &mut impl Write, ) -> Result<(), ErrorKind>

Encrypt data from a reader and write it in a writer. When working with small pieces of data, use Crypto::encrypt.

Example:

(See TmpDir)

use fencryption_lib::crypto::Crypto;
use fencryption_lib::tmp::TmpDir;

let my_super_key = b"this_is_super_secure";
let my_super_secret_message = b"hello :)";

let tmp_dir = TmpDir::new().unwrap();
let crypto = Crypto::new(my_super_key).unwrap();

tmp_dir.write_file("plain", my_super_secret_message).unwrap();

crypto
    .encrypt_io(
        &mut tmp_dir.open_readable("plain").unwrap(),
        &mut tmp_dir.create_file("enc").unwrap(),
    )
    .unwrap();
Source

pub fn decrypt_io( &self, source: &mut impl Read, dest: &mut impl Write, ) -> Result<(), ErrorKind>

Decrypt data from a reader and write it in a writer. When working with small pieces of data, use Crypto::decrypt.

Example:

(See TmpDir)

use fencryption_lib::crypto::Crypto;
use fencryption_lib::tmp::TmpDir;

let my_super_key = b"this_is_super_secure";
let my_super_secret_message = b"hello :)";

let tmp_dir = TmpDir::new().unwrap();
let crypto = Crypto::new(my_super_key).unwrap();

tmp_dir.write_file("plain", my_super_secret_message).unwrap();

crypto
    .encrypt_io(
        &mut tmp_dir.open_readable("plain").unwrap(),
        &mut tmp_dir.create_file("enc").unwrap(),
    )
    .unwrap();
crypto
    .decrypt_io(
        &mut tmp_dir.open_readable("enc").unwrap(),
        &mut tmp_dir.create_file("dec").unwrap(),
    )
    .unwrap();

assert_eq!(tmp_dir.read_file("dec").unwrap(), my_super_secret_message[..]);

Trait Implementations§

Source§

impl Clone for Crypto

Source§

fn clone(&self) -> Crypto

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

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

Performs copy-assignment from source. Read more

Auto Trait Implementations§

§

impl Freeze for Crypto

§

impl RefUnwindSafe for Crypto

§

impl Send for Crypto

§

impl Sync for Crypto

§

impl Unpin for Crypto

§

impl UnwindSafe for Crypto

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

Source§

type Output = T

Should always be Self
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.
Source§

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

Source§

fn vzip(self) -> V