-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9318475
commit a35fb8a
Showing
2 changed files
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
extern crate aes; | ||
extern crate block_modes; | ||
extern crate hex; | ||
extern crate hex_literal; | ||
|
||
use aes::Aes128; | ||
use block_modes::block_padding::Pkcs7; | ||
use block_modes::{BlockMode, Cbc}; | ||
use hex_literal::hex; | ||
|
||
type Aes128Cbc = Cbc<Aes128, Pkcs7>; | ||
|
||
fn encrypt(key: &[u8], iv: &[u8], plaintext: &[u8]) -> Vec<u8> { | ||
let cipher = Aes128Cbc::new_from_slices(key, iv).unwrap(); | ||
let mut buffer = [0u8; 32]; | ||
let pos = plaintext.len(); | ||
buffer[..pos].copy_from_slice(plaintext); | ||
cipher.encrypt(&mut buffer, pos).unwrap().to_vec() | ||
} | ||
|
||
fn decrypt(key: &[u8], iv: &[u8], ciphertext: &[u8]) -> Vec<u8> { | ||
let cipher = Aes128Cbc::new_from_slices(key, iv).unwrap(); | ||
let mut buffer = [0u8; 32]; | ||
buffer[..ciphertext.len()].copy_from_slice(ciphertext); | ||
cipher.decrypt(&mut buffer).unwrap().to_vec() | ||
} | ||
|
||
#[test] | ||
fn test_encrypt() { | ||
let key = hex!("616c616465656e6d61646166616b6161"); | ||
let iv = hex!("696c6f766573616e6477696368657321"); | ||
let plaintext = b"hello world AES!"; | ||
|
||
let ciphertext = encrypt(&key, &iv, plaintext); | ||
let expected_ciphertext = | ||
hex!("DD27193726EB570DED5437020239B65285C54DC862B88EBB42B8F75C03D5F526"); | ||
|
||
assert_eq!( | ||
hex::encode(&ciphertext[..16]), | ||
hex::encode(&expected_ciphertext[..16]) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_decrypt() { | ||
let key = hex!("616c616465656e6d61646166616b6161"); | ||
let iv = hex!("696c6f766573616e6477696368657321"); | ||
let ciphertext = hex!("DD27193726EB570DED5437020239B65285C54DC862B88EBB42B8F75C03D5F526"); | ||
|
||
let decrypted_ciphertext = decrypt(&key, &iv, &ciphertext); | ||
let expected_plaintext = b"hello world AES!"; | ||
|
||
assert_eq!( | ||
String::from_utf8_lossy(&decrypted_ciphertext[..expected_plaintext.len()]), | ||
String::from_utf8_lossy(expected_plaintext) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
mod aes_test; | ||
mod api_call_test; | ||
mod async_get_request_test; | ||
mod blocking_get_request_test; | ||
|