feat: add dependency aes-gcm-stream
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#[test]
|
||||
fn test_aes_gcm_01() {
|
||||
use aes_gcm::{Aes256Gcm, Key, KeyInit, Nonce};
|
||||
use aes_gcm::aead::{Aead};
|
||||
use aes_gcm_stream::Aes256GcmStreamEncryptor;
|
||||
|
||||
let data_key = hex::decode("0001020304050607080910111213141516171819202122232425262728293031").unwrap();
|
||||
let nonce = hex::decode("000102030405060708091011").unwrap();
|
||||
@@ -12,16 +11,21 @@ fn test_aes_gcm_01() {
|
||||
let plain_text2 = "This is a test message.".as_bytes();
|
||||
let encrypted_text2 = "c0e45407290878b0426fea4c09597ce323b056f975c63cce6c8da516c2a78c7d71b590c869cf92";
|
||||
|
||||
let key = Key::<Aes256Gcm>::from_slice(&data_key);
|
||||
let nonce = Nonce::from_slice(&nonce);
|
||||
let key256: [u8; 32] = data_key.as_slice().try_into().unwrap();
|
||||
{
|
||||
let mut aes256_gcm = Aes256Gcm::new(&key);
|
||||
let encrypted = aes256_gcm.encrypt(&nonce, plain_text1).unwrap();
|
||||
let mut encryptor = Aes256GcmStreamEncryptor::new(key256.clone(), &nonce);
|
||||
let mut encrypted = encryptor.update(plain_text1);
|
||||
let (last_block, tag) = encryptor.finalize();
|
||||
encrypted.extend_from_slice(&last_block);
|
||||
encrypted.extend_from_slice(&tag);
|
||||
assert_eq!(encrypted_text1, hex::encode(&encrypted));
|
||||
}
|
||||
{
|
||||
let mut aes256_gcm = Aes256Gcm::new(&key);
|
||||
let encrypted = aes256_gcm.encrypt(&nonce, plain_text2).unwrap();
|
||||
let mut encryptor = Aes256GcmStreamEncryptor::new(key256.clone(), &nonce);
|
||||
let mut encrypted = encryptor.update(plain_text2);
|
||||
let (last_block, tag) = encryptor.finalize();
|
||||
encrypted.extend_from_slice(&last_block);
|
||||
encrypted.extend_from_slice(&tag);
|
||||
assert_eq!(encrypted_text2, hex::encode(&encrypted));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user