Struct fencryption_lib::crypto::Crypto
source · pub struct Crypto { /* private fields */ }Implementations
sourceimpl 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]>,
Creates a new Crypto instance, the given key will be used for every operation performed.
sourcepub fn encrypt_with_nonce(
&self,
plaintext: &[u8],
iv: &[u8]
) -> Result<Vec<u8>, ErrorKind>
pub fn encrypt_with_nonce(
&self,
plaintext: &[u8],
iv: &[u8]
) -> Result<Vec<u8>, ErrorKind>
Basic function to encrypt.
sourcepub fn decrypt_with_nonce(
&self,
ciphertext: &[u8],
iv: &[u8]
) -> Result<Vec<u8>, ErrorKind>
pub fn decrypt_with_nonce(
&self,
ciphertext: &[u8],
iv: &[u8]
) -> Result<Vec<u8>, ErrorKind>
Basic function to decrypt.
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_stream(
&self,
source: &mut File,
dest: &mut File
) -> Result<(), ErrorKind>
pub fn encrypt_stream(
&self,
source: &mut File,
dest: &mut File
) -> Result<(), ErrorKind>
Encrypt a stream from a source and a destination (both std::fs::File).
Example:
use fencryption_lib::crypto::Crypto;
use fencryption_lib::tmp_dir::TmpDir;
let my_super_key = b"this_is_super_secure";
let my_super_secret_message = b"hello :)";
let crypto = Crypto::new(my_super_key).unwrap();
// Creates a temporary directory
let tmp_dir = TmpDir::new().unwrap();
// tmp_dir.write_file is akin to std::fs::write
tmp_dir
.write_file("plain", my_super_secret_message)
.unwrap();
crypto
.encrypt_stream(
// tmp_dir.open_file is akin to std::fs::File::open
&mut tmp_dir.open_file("plain").unwrap(),
// tmp_dir.create_file is akin to std::fs::File::create
&mut tmp_dir.create_file("enc").unwrap(),
)
.unwrap();sourcepub fn decrypt_stream(
&self,
source: &mut File,
dest: &mut File
) -> Result<(), ErrorKind>
pub fn decrypt_stream(
&self,
source: &mut File,
dest: &mut File
) -> Result<(), ErrorKind>
Decrypt a stream from a source and a destination (both std::fs::File).
Example:
use fencryption_lib::crypto::Crypto;
use fencryption_lib::tmp_dir::TmpDir;
let my_super_key = b"this_is_super_secure";
let my_super_secret_message = b"hello :)";
let crypto = Crypto::new(my_super_key).unwrap();
// Creates a temporary directory
let tmp_dir = TmpDir::new().unwrap();
// tmp_dir.write_file is akin to std::fs::write
tmp_dir
.write_file("plain", my_super_secret_message)
.unwrap();
crypto
.encrypt_stream(
// tmp_dir.open_file is akin to std::fs::File::open
&mut tmp_dir.open_file("plain").unwrap(),
// tmp_dir.create_file is akin to std::fs::File::create
&mut tmp_dir.create_file("enc").unwrap(),
)
.unwrap();
crypto
.decrypt_stream(
// tmp_dir.open_file is akin to std::fs::File::open
&mut tmp_dir.open_file("enc").unwrap(),
// tmp_dir.create_file is akin to std::fs::File::create
&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 RefUnwindSafe for Crypto
impl Send for Crypto
impl Sync for Crypto
impl Unpin for Crypto
impl UnwindSafe for Crypto
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more