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>
pub fn new<K>(key: K) -> Result<Crypto, ErrorKind>
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>
pub fn encrypt<P>(&self, plain: P) -> Result<Vec<u8>, ErrorKind>
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>
pub fn decrypt<E>(&self, enc: E) -> Result<Vec<u8>, ErrorKind>
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[..]);
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more