feat: v1.9.18

This commit is contained in:
2025-12-28 20:18:39 +08:00
parent 3812001474
commit cdec79e4dc
7 changed files with 54 additions and 37 deletions

2
Cargo.lock generated
View File

@@ -2077,7 +2077,7 @@ dependencies = [
[[package]] [[package]]
name = "tiny-encrypt" name = "tiny-encrypt"
version = "1.9.18" version = "1.9.19"
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.18" version = "1.9.19"
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

@@ -54,6 +54,9 @@ pub struct CmdConfig {
/// Temporary key output /// Temporary key output
#[arg(long)] #[arg(long)]
pub temporary_key: bool, pub temporary_key: bool,
/// Hide __all__
#[arg(long)]
pub hide_all: bool,
/// Encryption profile (use default when --key-filter is assigned) /// Encryption profile (use default when --key-filter is assigned)
#[arg(long, short = 'p')] #[arg(long, short = 'p')]
pub profile: Option<String>, pub profile: Option<String>,
@@ -129,7 +132,7 @@ fn strip_field(kid: &str, max_len: usize) -> String {
} }
} }
fn config_profiles(cmd_version: &CmdConfig, config: &TinyEncryptConfig) -> XResult<()> { fn config_profiles(cmd_config: &CmdConfig, config: &TinyEncryptConfig) -> XResult<()> {
let mut reverse_map = HashMap::new(); let mut reverse_map = HashMap::new();
if let Some(profiles) = &config.profiles { if let Some(profiles) = &config.profiles {
for (p, v) in profiles { for (p, v) in profiles {
@@ -148,6 +151,9 @@ fn config_profiles(cmd_version: &CmdConfig, config: &TinyEncryptConfig) -> XResu
let mut ps: Vec<_> = pvs.iter().map(|pv| pv.0).collect(); let mut ps: Vec<_> = pvs.iter().map(|pv| pv.0).collect();
ps.sort(); ps.sort();
let pp = ps.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(", "); let pp = ps.iter().map(|s| s.to_string()).collect::<Vec<_>>().join(", ");
if cmd_config.hide_all && pp == "__all__" {
continue;
}
let kids = pvs[0].1; let kids = pvs[0].1;
let mut ks = Vec::with_capacity(kids.len()); let mut ks = Vec::with_capacity(kids.len());
for kid in kids { for kid in kids {
@@ -156,7 +162,7 @@ fn config_profiles(cmd_version: &CmdConfig, config: &TinyEncryptConfig) -> XResu
ks.push(format!("[ERROR] Key not found: {}", kid)); ks.push(format!("[ERROR] Key not found: {}", kid));
} }
Some(envelop) => { Some(envelop) => {
let kid = if cmd_version.show_kid { let kid = if cmd_config.show_kid {
format!("Kid: {}", envelop.kid) format!("Kid: {}", envelop.kid)
} else { } else {
envelop.sid.as_ref() envelop.sid.as_ref()

View File

@@ -88,6 +88,7 @@ pub fn keychain_key_se(cmd_init_keychain: CmdInitKeychain) -> XResult<()> {
desc: Some("Keychain Secure Enclave".to_string()), desc: Some("Keychain Secure Enclave".to_string()),
args: Some(vec![saved_arg0]), args: Some(vec![saved_arg0]),
public_part: public_key_hex, public_part: public_key_hex,
profiles: None,
}; };
information!("Config envelop:\n{}", serde_json::to_string_pretty(&config_envelop).unwrap()); information!("Config envelop:\n{}", serde_json::to_string_pretty(&config_envelop).unwrap());
@@ -175,6 +176,7 @@ pub fn keychain_key_static(cmd_init_keychain: CmdInitKeychain) -> XResult<()> {
desc: Some("Keychain static".to_string()), desc: Some("Keychain static".to_string()),
args: Some(vec![keychain_key.to_str()]), args: Some(vec![keychain_key.to_str()]),
public_part: public_key_hex, public_part: public_key_hex,
profiles: None,
}; };
information!("Config envelop:\n{}", serde_json::to_string_pretty(&config_envelop).unwrap()); information!("Config envelop:\n{}", serde_json::to_string_pretty(&config_envelop).unwrap());

View File

@@ -69,6 +69,7 @@ pub fn init_piv(cmd_init_piv: CmdInitPiv) -> XResult<()> {
slot_id_hex.clone() slot_id_hex.clone()
]), ]),
public_part: public_key_point_hex, public_part: public_key_point_hex,
profiles: None,
}; };
information!("Config envelop:\n{}", serde_json::to_string_pretty(&config_envelop).unwrap()); information!("Config envelop:\n{}", serde_json::to_string_pretty(&config_envelop).unwrap());
@@ -84,6 +85,7 @@ pub fn init_piv(cmd_init_piv: CmdInitPiv) -> XResult<()> {
slot_id_hex.clone() slot_id_hex.clone()
]), ]),
public_part: util::to_pem(&spki, "PUBLIC KEY"), public_part: util::to_pem(&spki, "PUBLIC KEY"),
profiles: None,
}; };
information!("Config envelop:\n{}", serde_json::to_string_pretty(&config_envelop).unwrap()); information!("Config envelop:\n{}", serde_json::to_string_pretty(&config_envelop).unwrap());

View File

@@ -72,6 +72,7 @@ pub struct TinyEncryptConfigEnvelop {
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub args: Option<Vec<String>>, pub args: Option<Vec<String>>,
pub public_part: String, pub public_part: String,
pub profiles: Option<Vec<String>>,
} }
impl TinyEncryptConfig { impl TinyEncryptConfig {
@@ -123,26 +124,9 @@ impl TinyEncryptConfig {
file file
); );
debugging!("Config: {:#?}", config); debugging!("Config: {:#?}", config);
let mut config = load_includes_and_merge(config); let config = load_includes_and_merge(config);
debugging!("Final config: {:#?}", config); debugging!("Final config: {:#?}", config);
if let Some(profiles) = config.profiles {
let mut splited_profiles = HashMap::new();
for (k, v) in profiles.into_iter() {
if !k.contains(',') {
splited_profiles.insert(k, v);
} else {
k.split(',')
.map(|k| k.trim())
.filter(|k| !k.is_empty())
.for_each(|k| {
splited_profiles.insert(k.to_string(), v.clone());
});
}
}
config.profiles = Some(splited_profiles);
}
if let Some(environment) = &config.environment { if let Some(environment) = &config.environment {
for (k, v) in environment { for (k, v) in environment {
let v = match v { let v = match v {
@@ -364,14 +348,37 @@ pub fn load_includes_and_merge(mut config: TinyEncryptConfig) -> TinyEncryptConf
} }
config.envelops.push(sub_envelop.clone()); config.envelops.push(sub_envelop.clone());
} }
// deal with envelop profiles
let mut sub_profiles: HashMap<String, Vec<String>> = match &sub_config.profiles {
None => HashMap::new(),
Some(sub_profiles) => sub_profiles.clone(),
};
for envelop in &sub_config.envelops {
if let Some(profiles) = &envelop.profiles {
let kid = envelop.kid.clone();
for profile in profiles {
match sub_profiles.get_mut(profile) {
None => {
sub_profiles.insert(profile.clone(), vec![kid.clone()]);
}
Some(kids) => {
if !kids.contains(&kid) {
kids.push(kid.clone());
}
}
}
}
}
}
// merge profiles // merge profiles
if let Some(sub_profiles) = &sub_config.profiles {
match &mut config.profiles { match &mut config.profiles {
None => { None => {
config.profiles = Some(sub_profiles.clone()); config.profiles = Some(sub_profiles.clone());
} }
Some(profiles) => { Some(profiles) => {
for (k, v) in sub_profiles { for (k, v) in &sub_profiles {
match profiles.get_mut(k) { match profiles.get_mut(k) {
None => { None => {
profiles.insert(k.clone(), v.clone()); profiles.insert(k.clone(), v.clone());
@@ -387,7 +394,6 @@ pub fn load_includes_and_merge(mut config: TinyEncryptConfig) -> TinyEncryptConf
} }
} }
} }
}
if let Some(profiles) = &mut config.profiles { if let Some(profiles) = &mut config.profiles {
let all_key_ids = config.envelops.iter().map(|e| e.kid.clone()).collect::<Vec<_>>(); let all_key_ids = config.envelops.iter().map(|e| e.kid.clone()).collect::<Vec<_>>();
if profiles.contains_key("__all__") { if profiles.contains_key("__all__") {

View File

@@ -51,6 +51,7 @@ pub fn deserialize_config_envelop(k: &str) -> XResult<TinyEncryptConfigEnvelop>
desc: None, desc: None,
args: None, args: None,
public_part: decode(k_parts[4])?, public_part: decode(k_parts[4])?,
profiles: None,
}) })
} }