feat: v0.5.3, fix compress issue, supports env TINY_ENCRYPT_DEFAULT_ALGORITHM
This commit is contained in:
@@ -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];
|
||||
|
||||
Reference in New Issue
Block a user