feat: v0.5.3, fix compress issue, supports env TINY_ENCRYPT_DEFAULT_ALGORITHM

This commit is contained in:
2023-10-25 23:16:20 +08:00
parent d5cb25cc6a
commit c05cf1a7cf
9 changed files with 65 additions and 28 deletions

View File

@@ -3,7 +3,7 @@ use chacha20_poly1305_stream::{ChaCha20Poly1305StreamDecryptor, ChaCha20Poly1305
use rust_util::{opt_result, simple_error, XResult};
use zeroize::Zeroize;
use crate::consts;
use crate::{consts, util_env};
#[derive(Debug, Copy, Clone)]
pub enum Cryptor {
@@ -152,6 +152,20 @@ impl Decryptor for ChaCha20Poly1305Decryptor {
}
}
#[allow(clippy::redundant_closure)]
pub fn get_cryptor_by_encryption_algorithm(encryption_algorithm: &Option<String>) -> XResult<Cryptor> {
let encryption_algorithm = encryption_algorithm.as_deref()
.or_else(|| util_env::get_default_encryption_algorithm())
.unwrap_or(consts::TINY_ENC_AES_GCM)
.to_lowercase();
let cryptor = match encryption_algorithm.as_str() {
"aes" | "aes/gcm" => Cryptor::Aes256Gcm,
"chacha20" | "chacha20/poly1305" => Cryptor::ChaCha20Poly1305,
_ => return simple_error!("Unknown encryption algorithm: {}, should be AES or CHACHA20", encryption_algorithm),
};
Ok(cryptor)
}
#[test]
fn test_cryptor() {
let key = [0u8; 32];