feat: v1.7.14, support auto compress exts

This commit is contained in:
2024-09-22 01:09:16 +08:00
parent ac727f082e
commit 568022e655
5 changed files with 32 additions and 11 deletions

2
Cargo.lock generated
View File

@@ -1855,7 +1855,7 @@ dependencies = [
[[package]]
name = "tiny-encrypt"
version = "1.7.13"
version = "1.7.14"
dependencies = [
"aes-gcm-stream",
"base64",

View File

@@ -1,6 +1,6 @@
[package]
name = "tiny-encrypt"
version = "1.7.13"
version = "1.7.14"
edition = "2021"
license = "MIT"
description = "A simple and tiny file encrypt tool"

View File

@@ -153,6 +153,7 @@ Environment
| TINY_ENCRYPT_PIN | PIV Card PIN |
| TINY_ENCRYPT_KEY_ID | Default Key ID |
| TINY_ENCRYPT_AUTO_SELECT_KEY_IDS | Auto select Key IDs |
| TINY_ENCRYPT_AUTO_COMPRESS_EXTS | Auto compress file exts |
| TINY_ENCRYPT_PIN_ENTRY | PIN entry command cli |
| SECURE_EDITOR | Secure Editor |
| EDITOR | Editor (Plaintext) |

View File

@@ -7,10 +7,9 @@ use std::time::Instant;
use clap::Args;
use flate2::Compression;
use rsa::Pkcs1v15Encrypt;
use rust_util::{debugging, failure, iff, information, opt_result, simple_error, success, util_size, warning, XResult};
use rust_util::util_time::UnixEpochTime;
use rust_util::{debugging, failure, iff, information, opt_result, simple_error, success, util_size, warning, XResult};
use crate::{crypto_cryptor, crypto_simple, util, util_enc_file, util_env, util_gpg};
use crate::compress::GzStreamEncoder;
use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop};
use crate::consts::{ENC_AES256_GCM_KYBER1204, ENC_AES256_GCM_P256, ENC_AES256_GCM_P384, ENC_AES256_GCM_X25519, ENC_CHACHA20_POLY1305_KYBER1204, ENC_CHACHA20_POLY1305_P256, ENC_CHACHA20_POLY1305_P384, ENC_CHACHA20_POLY1305_X25519, SALT_COMMENT, TINY_ENC_CONFIG_FILE, TINY_ENC_FILE_EXT, TINY_ENC_PEM_FILE_EXT, TINY_ENC_PEM_NAME};
@@ -24,6 +23,7 @@ use crate::util_ecdh::{ecdh_kyber1024, ecdh_p256, ecdh_p384, ecdh_x25519};
use crate::util_progress::Progress;
use crate::util_rsa;
use crate::wrap_key::{WrapKey, WrapKeyHeader};
use crate::{crypto_cryptor, crypto_simple, util, util_enc_file, util_env, util_gpg};
#[derive(Debug, Args)]
pub struct CmdEncrypt {
@@ -185,7 +185,8 @@ pub fn encrypt_single_file_out(path: &PathBuf, path_out: &str, envelops: &[&Tiny
let enc_encrypted_meta_bytes = opt_result!(enc_encrypted_meta.seal(
cryptor, &key_nonce), "Seal enc-encrypted-meta failed: {}");
let compress_level = get_compress_level(cmd_encrypt, cmd_encrypt.pem_output);
let compress_level = get_compress_level(cmd_encrypt, &path_display, cmd_encrypt.pem_output);
debugging!("Compress level: {:?}", compress_level);
let enc_metadata = EncMetadata {
comment: cmd_encrypt.comment.clone(),
@@ -412,8 +413,16 @@ fn encrypt_envelop_gpg(key: &[u8], envelop: &TinyEncryptConfigEnvelop) -> XResul
})
}
fn get_compress_level(cmd_encrypt: &CmdEncrypt, pem_output: bool) -> Option<u32> {
if cmd_encrypt.compress || util_env::get_default_compress().unwrap_or(false) {
fn get_compress_level(cmd_encrypt: &CmdEncrypt, path: &str, pem_output: bool) -> Option<u32> {
let mut auto_compress = false;
let path_parts = path.split(".").collect::<Vec<_>>();
let path_ext = path_parts[path_parts.len() - 1].to_lowercase();
if let Some(auto_compress_file_exts) = util_env::get_auto_compress_file_exts() {
auto_compress = auto_compress_file_exts.contains(&path_ext);
debugging!("File ext: {} matches auto compress exts: {:?}", path_ext, auto_compress_file_exts);
}
if auto_compress || cmd_encrypt.compress || util_env::get_default_compress().unwrap_or(false) {
Some(cmd_encrypt.compress_level.unwrap_or_else(|| Compression::default().level()))
} else if pem_output {
Some(Compression::best().level())

View File

@@ -1,7 +1,7 @@
use std::env;
use rust_util::{debugging, util_env, warning};
use rust_util::util_env as rust_util_env;
use rust_util::{debugging, util_env, warning};
use crate::consts;
@@ -12,6 +12,7 @@ pub const TINY_ENCRYPT_ENV_USE_DIALOGUER: &str = "TINY_ENCRYPT_USE_DIALOGUER";
pub const TINY_ENCRYPT_ENV_PIN: &str = "TINY_ENCRYPT_PIN";
pub const TINY_ENCRYPT_ENV_KEY_ID: &str = "TINY_ENCRYPT_KEY_ID";
pub const TINY_ENCRYPT_ENV_AUTO_SELECT_KEY_IDS: &str = "TINY_ENCRYPT_AUTO_SELECT_KEY_IDS";
pub const TINY_ENCRYPT_EVN_AUTO_COMPRESS_EXTS: &str = "TINY_ENCRYPT_AUTO_COMPRESS_EXTS";
pub const TINY_ENCRYPT_ENV_GPG_COMMAND: &str = "TINY_ENCRYPT_GPG_COMMAND";
pub const TINY_ENCRYPT_ENV_NO_DEFAULT_PIN_HINT: &str = "TINY_ENCRYPT_NO_DEFAULT_PIN_HINT";
pub const TINY_ENCRYPT_ENV_PIN_ENTRY: &str = "TINY_ENCRYPT_PIN_ENTRY";
@@ -47,9 +48,11 @@ pub fn get_pin_entry() -> Option<String> {
}
pub fn get_auto_select_key_ids() -> Option<Vec<String>> {
env::var(TINY_ENCRYPT_ENV_AUTO_SELECT_KEY_IDS).ok().map(|key_ids| {
key_ids.split(',').map(ToString::to_string).collect::<Vec<_>>()
})
get_env_with_split(TINY_ENCRYPT_ENV_AUTO_SELECT_KEY_IDS)
}
pub fn get_auto_compress_file_exts() -> Option<Vec<String>> {
get_env_with_split(TINY_ENCRYPT_EVN_AUTO_COMPRESS_EXTS)
}
pub fn get_default_compress() -> Option<bool> {
@@ -67,3 +70,11 @@ pub fn get_no_default_pin_hint() -> bool {
pub fn get_use_dialoguer() -> bool {
rust_util_env::is_env_on(TINY_ENCRYPT_ENV_USE_DIALOGUER)
}
fn get_env_with_split(env_name: &str) -> Option<Vec<String>> {
let val = env::var(env_name).ok();
debugging!("Environment variable {} = {:?}", env_name, &val);
val.map(|env_values| {
env_values.split(',').map(ToString::to_string).collect::<Vec<_>>()
})
}