feat: v1.9.8
This commit is contained in:
@@ -49,6 +49,10 @@ pub struct CmdSimpleEncrypt {
|
||||
#[arg(long, short = 'A')]
|
||||
pub password: Option<String>,
|
||||
|
||||
/// Direct output result value
|
||||
#[arg(long)]
|
||||
pub outputs_password: bool,
|
||||
|
||||
/// Direct output result value
|
||||
#[arg(long)]
|
||||
pub direct_output: bool,
|
||||
@@ -84,6 +88,10 @@ pub struct CmdSimpleDecrypt {
|
||||
#[arg(long, short = 'A')]
|
||||
pub password: Option<String>,
|
||||
|
||||
/// Direct output result value
|
||||
#[arg(long)]
|
||||
pub outputs_password: bool,
|
||||
|
||||
/// Direct output result value
|
||||
#[arg(long)]
|
||||
pub direct_output: bool,
|
||||
@@ -122,6 +130,8 @@ pub struct CmdResult {
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub message: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub password: Option<String>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub result: Option<String>,
|
||||
}
|
||||
|
||||
@@ -130,14 +140,16 @@ impl CmdResult {
|
||||
Self {
|
||||
code,
|
||||
message: Some(message.to_string()),
|
||||
password: None,
|
||||
result: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn success(result: &str) -> Self {
|
||||
pub fn success(result: &str, password: Option<String>) -> Self {
|
||||
Self {
|
||||
code: 0,
|
||||
message: None,
|
||||
password,
|
||||
result: Some(result.to_string()),
|
||||
}
|
||||
}
|
||||
@@ -205,12 +217,16 @@ pub fn inner_simple_encrypt(cmd_simple_encrypt: CmdSimpleEncrypt) -> XResult<()>
|
||||
);
|
||||
|
||||
let with_pbkdf_encryption = cmd_simple_encrypt.with_pbkdf_encryption || cmd_simple_encrypt.password.is_some();
|
||||
let mut outputs_password = None;
|
||||
if with_pbkdf_encryption {
|
||||
let password = util::read_password(&cmd_simple_encrypt.password)?;
|
||||
simple_encrypt_result = SimplePbkdfEncryptionV1::encrypt(&password, simple_encrypt_result.as_bytes())?.to_string();
|
||||
if cmd_simple_encrypt.outputs_password {
|
||||
outputs_password = Some(password);
|
||||
}
|
||||
}
|
||||
|
||||
CmdResult::success(&simple_encrypt_result).print_exit(cmd_simple_encrypt.direct_output);
|
||||
CmdResult::success(&simple_encrypt_result, outputs_password).print_exit(cmd_simple_encrypt.direct_output);
|
||||
}
|
||||
|
||||
#[cfg(feature = "decrypt")]
|
||||
@@ -231,11 +247,15 @@ pub fn inner_simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()>
|
||||
Some(value) => value,
|
||||
};
|
||||
|
||||
let mut outputs_password = None;
|
||||
if SimplePbkdfEncryptionV1::matches(&value) {
|
||||
let simple_pbkdf_encryption_v1: SimplePbkdfEncryptionV1 = value.as_str().try_into()?;
|
||||
let password = util::read_password(&cmd_simple_decrypt.password)?;
|
||||
let plaintext_bytes = simple_pbkdf_encryption_v1.decrypt(&password)?;
|
||||
value = opt_result!(String::from_utf8(plaintext_bytes), "Decrypt PBKDF encryption failed: {}");
|
||||
if cmd_simple_decrypt.outputs_password {
|
||||
outputs_password = Some(password);
|
||||
}
|
||||
}
|
||||
|
||||
let value_parts = value.trim().split(SIMPLE_ENCRYPTION_DOT).collect::<Vec<_>>();
|
||||
@@ -280,5 +300,5 @@ pub fn inner_simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()>
|
||||
"base64" => STANDARD.encode(&value),
|
||||
_ => return simple_error!("not supported output format: {}", output_format),
|
||||
};
|
||||
CmdResult::success(&value).print_exit(cmd_simple_decrypt.direct_output);
|
||||
CmdResult::success(&value, outputs_password).print_exit(cmd_simple_decrypt.direct_output);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ pub fn read_pin(pin: &Option<String>) -> XResult<String> {
|
||||
None => if is_use_default_pin() {
|
||||
"123456".into()
|
||||
} else {
|
||||
let pin_entry = util_env::get_pin_entry().unwrap_or_else(|| "pinentry".to_string());
|
||||
let pin_entry = util_env::get_default_pin_entry().unwrap_or_else(|| "pinentry".to_string());
|
||||
if let Some(mut input) = PassphraseInput::with_binary(pin_entry) {
|
||||
let secret = input
|
||||
.with_description("Please input your PIN.")
|
||||
@@ -66,7 +66,7 @@ pub fn read_password(password: &Option<String>) -> XResult<String> {
|
||||
let rpassword = match password {
|
||||
Some(pin) => pin.to_string(),
|
||||
None => {
|
||||
let pin_entry = util_env::get_pin_entry().unwrap_or_else(|| "pinentry".to_string());
|
||||
let pin_entry = util_env::get_default_pin_entry().unwrap_or_else(|| "pinentry".to_string());
|
||||
if let Some(mut input) = PassphraseInput::with_binary(pin_entry) {
|
||||
let secret = input
|
||||
.with_description("Please input your password.")
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use std::env;
|
||||
use std::{env, fs};
|
||||
|
||||
use rust_util::util_env as rust_util_env;
|
||||
use rust_util::{debugging, util_env, warning};
|
||||
@@ -43,6 +43,16 @@ pub fn get_gpg_cmd() -> Option<String> {
|
||||
env::var(TINY_ENCRYPT_ENV_GPG_COMMAND).ok()
|
||||
}
|
||||
|
||||
pub fn get_default_pin_entry() -> Option<String> {
|
||||
let default_pin_entry = "/usr/local/MacGPG2/libexec/pinentry-mac.app/Contents/MacOS/pinentry-mac";
|
||||
if let Ok(meta) = fs::metadata(default_pin_entry) {
|
||||
if meta.is_file() {
|
||||
return Some(default_pin_entry.to_string());
|
||||
}
|
||||
}
|
||||
get_pin_entry()
|
||||
}
|
||||
|
||||
pub fn get_pin_entry() -> Option<String> {
|
||||
env::var(TINY_ENCRYPT_ENV_PIN_ENTRY).ok()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user