Struct fencryption_lib::crypto::Crypto
source · pub struct Crypto { /* private fields */ }Expand description
A struct for encrypting/decrypting bytes or io streams.
Implementations§
source§impl Crypto
impl Crypto
sourcepub fn new<K>(key: K) -> Result<Crypto, ErrorKind>where
K: AsRef<[u8]>,
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.
sourcepub fn encrypt_with_iv(
&self,
plaintext: &[u8],
iv: &[u8]
) -> Result<Vec<u8>, ErrorKind>
pub fn encrypt_with_iv( &self, plaintext: &[u8], iv: &[u8] ) -> Result<Vec<u8>, ErrorKind>
Encrypt bytes with initialisation vector.
sourcepub fn decrypt_with_iv(
&self,
ciphertext: &[u8],
iv: &[u8]
) -> Result<Vec<u8>, ErrorKind>
pub fn decrypt_with_iv( &self, ciphertext: &[u8], iv: &[u8] ) -> Result<Vec<u8>, ErrorKind>
Decrypt bytes with initialisation vector.
sourcepub fn encrypt<P>(&self, plain: P) -> Result<Vec<u8>, ErrorKind>where
P: AsRef<[u8]>,
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);sourcepub fn decrypt<E>(&self, enc: E) -> Result<Vec<u8>, ErrorKind>where
E: AsRef<[u8]>,
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);sourcepub fn encrypt_io(
&self,
source: &mut impl Read,
dest: &mut impl Write
) -> Result<(), ErrorKind>
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();sourcepub fn decrypt_io(
&self,
source: &mut impl Read,
dest: &mut impl Write
) -> Result<(), ErrorKind>
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[..]);