feat: v1.9.17

This commit is contained in:
2025-10-17 19:20:41 +08:00
parent 0e6b590f32
commit a6397fc45a
9 changed files with 52 additions and 17 deletions

2
Cargo.lock generated
View File

@@ -2077,7 +2077,7 @@ dependencies = [
[[package]] [[package]]
name = "tiny-encrypt" name = "tiny-encrypt"
version = "1.9.16" version = "1.9.17"
dependencies = [ dependencies = [
"aes-gcm-stream", "aes-gcm-stream",
"base64 0.22.1", "base64 0.22.1",

View File

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

View File

@@ -63,7 +63,7 @@ pub struct CmdConfig {
} }
pub fn config(cmd_config: CmdConfig) -> XResult<()> { pub fn config(cmd_config: CmdConfig) -> XResult<()> {
let config = TinyEncryptConfig::load_default()?; let config = TinyEncryptConfig::load_default(&None)?;
if cmd_config.json { if cmd_config.json {
let mut config = config; let mut config = config;

View File

@@ -95,6 +95,10 @@ pub struct CmdDecrypt {
#[arg(long, short = 'A')] #[arg(long, short = 'A')]
pub digest_algorithm: Option<String>, pub digest_algorithm: Option<String>,
/// Config file or based64 encoded (starts with: base64:)
#[arg(long)]
pub config: Option<String>,
/// Files need to be decrypted /// Files need to be decrypted
pub paths: Vec<PathBuf>, pub paths: Vec<PathBuf>,
} }
@@ -108,7 +112,7 @@ impl Drop for CmdDecrypt {
pub fn decrypt(cmd_decrypt: CmdDecrypt) -> XResult<()> { pub fn decrypt(cmd_decrypt: CmdDecrypt) -> XResult<()> {
if cmd_decrypt.split_print { util_msg::set_logger_std_out(false); } if cmd_decrypt.split_print { util_msg::set_logger_std_out(false); }
debugging!("Cmd decrypt: {:?}", cmd_decrypt); debugging!("Cmd decrypt: {:?}", cmd_decrypt);
let config = TinyEncryptConfig::load_default().ok(); let config = TinyEncryptConfig::load_default(&cmd_decrypt.config).ok();
let start = Instant::now(); let start = Instant::now();
let mut succeed_count = 0; let mut succeed_count = 0;

View File

@@ -77,12 +77,16 @@ pub struct CmdEncrypt {
#[arg(long, short = 'A')] #[arg(long, short = 'A')]
pub encryption_algorithm: Option<String>, pub encryption_algorithm: Option<String>,
/// Config file or based64 encoded (starts with: base64:)
#[arg(long)]
pub config: Option<String>,
/// Files need to be decrypted /// Files need to be decrypted
pub paths: Vec<PathBuf>, pub paths: Vec<PathBuf>,
} }
pub fn encrypt(cmd_encrypt: CmdEncrypt) -> XResult<()> { pub fn encrypt(cmd_encrypt: CmdEncrypt) -> XResult<()> {
let config = TinyEncryptConfig::load_default()?; let config = TinyEncryptConfig::load_default(&cmd_encrypt.config)?;
debugging!("Found tiny encrypt config: {:?}", config); debugging!("Found tiny encrypt config: {:?}", config);
let mut envelops = config.find_envelops(&cmd_encrypt.profile, &cmd_encrypt.key_filter)?; let mut envelops = config.find_envelops(&cmd_encrypt.profile, &cmd_encrypt.key_filter)?;
debugging!("Found envelops: {:?}", envelops); debugging!("Found envelops: {:?}", envelops);

View File

@@ -29,6 +29,10 @@ pub struct CmdExecEnv {
#[arg(long, short = 's')] #[arg(long, short = 's')]
pub slot: Option<String>, pub slot: Option<String>,
/// Config file or based64 encoded (starts with: base64:)
#[arg(long)]
pub config: Option<String>,
/// Tiny encrypt file name /// Tiny encrypt file name
pub file_name: String, pub file_name: String,
@@ -45,7 +49,7 @@ impl Drop for CmdExecEnv {
pub fn exec_env(cmd_exec_env: CmdExecEnv) -> XResult<()> { pub fn exec_env(cmd_exec_env: CmdExecEnv) -> XResult<()> {
util_msg::set_logger_std_out(false); util_msg::set_logger_std_out(false);
debugging!("Cmd exec env: {:?}", cmd_exec_env); debugging!("Cmd exec env: {:?}", cmd_exec_env);
let config = TinyEncryptConfig::load_default().ok(); let config = TinyEncryptConfig::load_default(&cmd_exec_env.config).ok();
if cmd_exec_env.command_arguments.is_empty() { if cmd_exec_env.command_arguments.is_empty() {
return simple_error!("No commands assigned."); return simple_error!("No commands assigned.");
} }

View File

@@ -23,12 +23,16 @@ pub struct CmdInfo {
#[arg(long, short = 'M', default_value_t = false)] #[arg(long, short = 'M', default_value_t = false)]
pub raw_meta: bool, pub raw_meta: bool,
/// Config file or based64 encoded (starts with: base64:)
#[arg(long)]
pub config: Option<String>,
/// File /// File
pub paths: Vec<PathBuf>, pub paths: Vec<PathBuf>,
} }
pub fn info(cmd_info: CmdInfo) -> XResult<()> { pub fn info(cmd_info: CmdInfo) -> XResult<()> {
let config = TinyEncryptConfig::load_default().ok(); let config = TinyEncryptConfig::load_default(&cmd_info.config).ok();
for (i, path) in cmd_info.paths.iter().enumerate() { for (i, path) in cmd_info.paths.iter().enumerate() {
let path = config::resolve_path_namespace(&config, path, true); let path = config::resolve_path_namespace(&config, path, true);
if i > 0 { println!("{}", "-".repeat(88)); } if i > 0 { println!("{}", "-".repeat(88)); }

View File

@@ -54,6 +54,10 @@ pub struct CmdSimpleEncrypt {
#[arg(long, short = 'A')] #[arg(long, short = 'A')]
pub password: Option<String>, pub password: Option<String>,
/// Config file or based64 encoded (starts with: base64:)
#[arg(long)]
pub config: Option<String>,
/// Direct output result value /// Direct output result value
#[arg(long)] #[arg(long)]
pub outputs_password: bool, pub outputs_password: bool,
@@ -93,6 +97,10 @@ pub struct CmdSimpleDecrypt {
#[arg(long, short = 'A')] #[arg(long, short = 'A')]
pub password: Option<String>, pub password: Option<String>,
/// Config file or based64 encoded (starts with: base64:)
#[arg(long)]
pub config: Option<String>,
/// Direct output result value /// Direct output result value
#[arg(long)] #[arg(long)]
pub outputs_password: bool, pub outputs_password: bool,
@@ -193,7 +201,7 @@ pub fn simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()> {
} }
pub fn inner_simple_encrypt(cmd_simple_encrypt: CmdSimpleEncrypt) -> XResult<()> { pub fn inner_simple_encrypt(cmd_simple_encrypt: CmdSimpleEncrypt) -> XResult<()> {
let config = TinyEncryptConfig::load_default()?; let config = TinyEncryptConfig::load_default(&cmd_simple_encrypt.config)?;
debugging!("Found tiny encrypt config: {:?}", config); debugging!("Found tiny encrypt config: {:?}", config);
let mut envelops = config.find_envelops( let mut envelops = config.find_envelops(
@@ -248,7 +256,7 @@ pub fn inner_simple_encrypt(cmd_simple_encrypt: CmdSimpleEncrypt) -> XResult<()>
#[cfg(feature = "decrypt")] #[cfg(feature = "decrypt")]
pub fn inner_simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()> { pub fn inner_simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()> {
let config = TinyEncryptConfig::load_default().ok(); let config = TinyEncryptConfig::load_default(&cmd_simple_decrypt.config).ok();
let pin = cmd_simple_decrypt.pin.clone().or_else(util_env::get_pin); let pin = cmd_simple_decrypt.pin.clone().or_else(util_env::get_pin);
let slot = cmd_simple_decrypt.slot.clone(); let slot = cmd_simple_decrypt.slot.clone();

View File

@@ -3,12 +3,13 @@ use std::collections::HashMap;
use std::path::Path; use std::path::Path;
use std::path::PathBuf; use std::path::PathBuf;
use std::{env, fs}; use std::{env, fs};
use rust_util::util_env as rust_util_env; use rust_util::{util_env as rust_util_env};
use rust_util::util_file::resolve_file_path; use rust_util::util_file::resolve_file_path;
use rust_util::{debugging, opt_result, warning, XResult}; use rust_util::{debugging, opt_result, warning, XResult};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::consts::{ENV_TINY_ENC_CONFIG_FILE, TINY_ENC_CONFIG_FILE, TINY_ENC_CONFIG_FILE_2, TINY_ENC_CONFIG_FILE_3, TINY_ENC_FILE_EXT}; use crate::consts::{ENV_TINY_ENC_CONFIG_FILE, TINY_ENC_CONFIG_FILE, TINY_ENC_CONFIG_FILE_2, TINY_ENC_CONFIG_FILE_3, TINY_ENC_FILE_EXT};
use crate::spec::TinyEncryptEnvelopType; use crate::spec::TinyEncryptEnvelopType;
use crate::util::decode_base64;
/// Config file sample: /// Config file sample:
/// ~/.tinyencrypt/config-rs.json /// ~/.tinyencrypt/config-rs.json
@@ -74,14 +75,20 @@ pub struct TinyEncryptConfigEnvelop {
} }
impl TinyEncryptConfig { impl TinyEncryptConfig {
pub fn load_default() -> XResult<Self> { pub fn load_default(config: &Option<String>) -> XResult<Self> {
let resolved_file0 = rust_util_env::env_var(ENV_TINY_ENC_CONFIG_FILE); let resolved_file0 = config.clone().or_else(|| rust_util_env::env_var(ENV_TINY_ENC_CONFIG_FILE));
let resolved_file_1 = resolve_file_path(TINY_ENC_CONFIG_FILE); let resolved_file_1 = resolve_file_path(TINY_ENC_CONFIG_FILE);
let resolved_file_2 = resolve_file_path(TINY_ENC_CONFIG_FILE_2); let resolved_file_2 = resolve_file_path(TINY_ENC_CONFIG_FILE_2);
let resolved_file_3 = resolve_file_path(TINY_ENC_CONFIG_FILE_3); let resolved_file_3 = resolve_file_path(TINY_ENC_CONFIG_FILE_3);
if let Some(resolved_file) = resolved_file0 { if let Some(resolved_file) = resolved_file0 {
if resolved_file.starts_with("base64:") {
let decoded_resolved_bytes_result = decode_base64(&resolved_file.chars().skip(7).collect::<String>());
let decoded_resolved_bytes = opt_result!(decoded_resolved_bytes_result, "Decode base64 failed: {}");
let decoded_resolved_content = opt_result!(String::from_utf8(decoded_resolved_bytes), "Decode UTF-8 string failed: {}");
return Self::load_content(&decoded_resolved_content, "<env>");
}
debugging!("Found tiny encrypt config file: {}", &resolved_file); debugging!("Found tiny encrypt config file: {}", &resolved_file);
return Self::load(&resolved_file) return Self::load_file(&resolved_file)
} }
let config_file = if fs::metadata(&resolved_file_1).is_ok() { let config_file = if fs::metadata(&resolved_file_1).is_ok() {
debugging!("Load config from: {resolved_file_1}"); debugging!("Load config from: {resolved_file_1}");
@@ -96,18 +103,22 @@ impl TinyEncryptConfig {
warning!("Cannot find config file from:\n- {resolved_file_1}\n- {resolved_file_2}\n- {resolved_file_3}"); warning!("Cannot find config file from:\n- {resolved_file_1}\n- {resolved_file_2}\n- {resolved_file_3}");
resolved_file_1 resolved_file_1
}; };
Self::load(&config_file) Self::load_file(&config_file)
} }
pub fn load(file: &str) -> XResult<Self> { pub fn load_file(file: &str) -> XResult<Self> {
let resolved_file = resolve_file_path(file); let resolved_file = resolve_file_path(file);
let config_contents = opt_result!( let config_content = opt_result!(
fs::read_to_string(resolved_file), fs::read_to_string(resolved_file),
"Read config file: {}, failed: {}", "Read config file: {}, failed: {}",
file file
); );
Self::load_content(&config_content, file)
}
pub fn load_content(config_content: &str, file: &str) -> XResult<Self> {
let config: TinyEncryptConfig = opt_result!( let config: TinyEncryptConfig = opt_result!(
serde_json::from_str(&config_contents), serde_json::from_str(&config_content),
"Parse config file: {}, failed: {}", "Parse config file: {}, failed: {}",
file file
); );