feat: add auto select key ids
This commit is contained in:
@@ -111,7 +111,7 @@ pub fn decrypt(cmd_decrypt: CmdDecrypt) -> XResult<()> {
|
|||||||
"Decrypt {} succeed, cost {} ms{}",
|
"Decrypt {} succeed, cost {} ms{}",
|
||||||
path.to_str().unwrap_or("N/A"),
|
path.to_str().unwrap_or("N/A"),
|
||||||
start_decrypt_single.elapsed().as_millis(),
|
start_decrypt_single.elapsed().as_millis(),
|
||||||
iff!(len <= 0, "".to_string(), format!(", file size {}", util_size::get_display_size(len as i64)))
|
iff!(len == 0, "".to_string(), format!(", file size {}", util_size::get_display_size(len as i64)))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -650,6 +650,15 @@ pub fn select_envelop<'a>(meta: &'a TinyEncryptMeta, key_id: &Option<String>, co
|
|||||||
return Ok(selected_envelop);
|
return Ok(selected_envelop);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// auto select
|
||||||
|
if let Some(auto_select_key_ids) = util_env::get_auto_select_key_ids() {
|
||||||
|
for auto_select_key_id in auto_select_key_ids {
|
||||||
|
if let Some(envelop) = match_envelop_by_key_id(envelops, &Some(auto_select_key_id), config) {
|
||||||
|
return Ok(envelop);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
envelops.iter().enumerate().for_each(|(i, envelop)| {
|
envelops.iter().enumerate().for_each(|(i, envelop)| {
|
||||||
println_ex!("#{} {}", i + 1, util_envelop::format_envelop(envelop, config));
|
println_ex!("#{} {}", i + 1, util_envelop::format_envelop(envelop, config));
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ pub fn keychain_key_static(cmd_init_keychain: CmdInitKeychain) -> XResult<()> {
|
|||||||
let public_key = match sec_keychain.find_generic_password(service_name, key_name) {
|
let public_key = match sec_keychain.find_generic_password(service_name, key_name) {
|
||||||
Ok((static_x25519, _)) => {
|
Ok((static_x25519, _)) => {
|
||||||
warning!("Key already exists: {}.{}", service_name, key_name);
|
warning!("Key already exists: {}.{}", service_name, key_name);
|
||||||
let x25519_static_secret = X25519StaticSecret::parse_bytes(&static_x25519.as_ref())?;
|
let x25519_static_secret = X25519StaticSecret::parse_bytes(static_x25519.as_ref())?;
|
||||||
x25519_static_secret.to_public_key()?
|
x25519_static_secret.to_public_key()?
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ pub const TINY_ENCRYPT_ENV_DEFAULT_COMPRESS: &str = "TINY_ENCRYPT_DEFAULT_COMPRE
|
|||||||
pub const TINY_ENCRYPT_ENV_NO_PROGRESS: &str = "TINY_ENCRYPT_NO_PROGRESS";
|
pub const TINY_ENCRYPT_ENV_NO_PROGRESS: &str = "TINY_ENCRYPT_NO_PROGRESS";
|
||||||
pub const TINY_ENCRYPT_ENV_PIN: &str = "TINY_ENCRYPT_PIN";
|
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_KEY_ID: &str = "TINY_ENCRYPT_KEY_ID";
|
||||||
|
pub const TINY_ENCRYPT_ENV_AUTO_SELECT_KEY_IDS: &str = "TINY_ENCRYPT_AUTO_SELECT_KEY_IDS";
|
||||||
|
|
||||||
pub fn get_default_encryption_algorithm() -> Option<&'static str> {
|
pub fn get_default_encryption_algorithm() -> Option<&'static str> {
|
||||||
let env_default_algorithm = env::var(TINY_ENCRYPT_ENV_DEFAULT_ALGORITHM).ok();
|
let env_default_algorithm = env::var(TINY_ENCRYPT_ENV_DEFAULT_ALGORITHM).ok();
|
||||||
@@ -33,6 +34,12 @@ pub fn get_key_id() -> Option<String> {
|
|||||||
env::var(TINY_ENCRYPT_ENV_KEY_ID).ok()
|
env::var(TINY_ENCRYPT_ENV_KEY_ID).ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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<_>>()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_default_compress() -> Option<bool> {
|
pub fn get_default_compress() -> Option<bool> {
|
||||||
iff!(rust_util_env::is_env_off(TINY_ENCRYPT_ENV_DEFAULT_COMPRESS), Some(true), None)
|
iff!(rust_util_env::is_env_off(TINY_ENCRYPT_ENV_DEFAULT_COMPRESS), Some(true), None)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user