Function crypto_ext::symmetric::encryption::encrypt
source · pub fn encrypt(
params: EncryptionParameters,
data_to_encrypt: &[u8],
associated_data: &[u8]
) -> Result<Vec<u8>, String>
Expand description
Encrypts given byte array
Examples
use crypto_ext::symmetric::encryption::{decrypt, encrypt, get_decryption_params, get_encryption_params, setup};
#[test]
fn encryption() {
let data = "some data to encrypt".as_bytes();
let associated_data = "some unencrypted data that needs to be sent along the encrypted data and won't be changed during transmission by a hacker".as_bytes();
// path needs to be accessible by user with write permission for initial setup
// ideally for each encryption you need to setup unique folder for AES key and nonce
// do not reuse same setup with Encryption and Decryption parameters for multiple encryptions
let params_path = "/test/encryption_parameters/";
let (encryption_params, decryption_params) = setup(Some(params_path)).unwrap();
let encrypted = encrypt(encryption_params, data, associated_data).unwrap();
let decrypted = decrypt(decryption_params, encrypted.as_slice(), associated_data).unwrap();
assert_eq!(data, decrypted);
}