feat: add dependency aes-gcm-stream

This commit is contained in:
2023-09-02 01:04:46 +08:00
parent 3eb2c1f46d
commit b795ee782d
3 changed files with 494 additions and 495 deletions

968
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -9,6 +9,7 @@ description = "A simple and tiny file encrypt tool"
[dependencies]
aes-gcm = { version = "0.10.1", features = ["zeroize"] }
aes-gcm-stream = "0.2.0"
base64 = "0.21.0"
chrono = "0.4.23"
clap = { version = "4.1.4", features = ["derive"] }

View File

@@ -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));
}
}