feat: v1.8.1, add --direct-output for simple-encrypt/decrypt
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1873,7 +1873,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "tiny-encrypt"
|
||||
version = "1.8.0"
|
||||
version = "1.8.1"
|
||||
dependencies = [
|
||||
"aes-gcm-stream",
|
||||
"base64 0.22.1",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "tiny-encrypt"
|
||||
version = "1.8.0"
|
||||
version = "1.8.1"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
description = "A simple and tiny file encrypt tool"
|
||||
|
||||
@@ -313,8 +313,8 @@ fn run_file_editor_and_wait_content(editor: &str, temp_file: &PathBuf, secure_ed
|
||||
command.arg(temp_file.to_str().expect("Get temp file path failed."));
|
||||
if secure_editor {
|
||||
command.arg("aes-256-gcm");
|
||||
command.arg(&hex::encode(&temp_encryption_key_nonce.0));
|
||||
command.arg(&hex::encode(&temp_encryption_key_nonce.1));
|
||||
command.arg(hex::encode(&temp_encryption_key_nonce.0));
|
||||
command.arg(hex::encode(&temp_encryption_key_nonce.1));
|
||||
if readonly { command.env("READONLY", "true"); }
|
||||
}
|
||||
debugging!("Run cmd: {:?}", command);
|
||||
|
||||
@@ -8,6 +8,8 @@ use base64::Engine;
|
||||
use clap::Args;
|
||||
use rust_util::{debugging, opt_result, simple_error, XResult};
|
||||
use serde::Serialize;
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use std::process::exit;
|
||||
|
||||
// Reference: https://git.hatter.ink/hatter/tiny-encrypt-rs/issues/3
|
||||
@@ -39,6 +41,10 @@ pub struct CmdSimpleEncrypt {
|
||||
/// Encrypt value in hex
|
||||
#[arg(long)]
|
||||
pub value_hex: Option<String>,
|
||||
|
||||
/// Direct output result value
|
||||
#[arg(long)]
|
||||
pub direct_output: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Args)]
|
||||
@@ -66,6 +72,10 @@ pub struct CmdSimpleDecrypt {
|
||||
/// Decrypt result output format (plain, hex, bse64)
|
||||
#[arg(long, short = 'o')]
|
||||
pub output_format: Option<String>,
|
||||
|
||||
/// Direct output result value
|
||||
#[arg(long)]
|
||||
pub direct_output: bool,
|
||||
}
|
||||
|
||||
impl CmdSimpleEncrypt {
|
||||
@@ -121,23 +131,34 @@ impl CmdResult {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_exit(&self) -> ! {
|
||||
let result = serde_json::to_string_pretty(self).unwrap();
|
||||
println!("{}", result);
|
||||
pub fn print_exit(&self, direct_output_value: bool) -> ! {
|
||||
// TODO direct_output_value
|
||||
if direct_output_value {
|
||||
if self.code == 0 {
|
||||
print!("{}", self.result.as_deref().unwrap());
|
||||
} else {
|
||||
println!("{}", self.message.as_deref().unwrap_or("unknown error"));
|
||||
}
|
||||
} else {
|
||||
let result = serde_json::to_string_pretty(self).unwrap();
|
||||
println!("{}", result);
|
||||
}
|
||||
exit(self.code)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn simple_encrypt(cmd_simple_encrypt: CmdSimpleEncrypt) -> XResult<()> {
|
||||
let direct_output = cmd_simple_encrypt.direct_output;
|
||||
if let Err(inner_result_error) = inner_simple_encrypt(cmd_simple_encrypt) {
|
||||
CmdResult::fail(-1, &format!("{}", inner_result_error)).print_exit();
|
||||
CmdResult::fail(-1, &format!("{}", inner_result_error)).print_exit(direct_output);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()> {
|
||||
let direct_output = cmd_simple_decrypt.direct_output;
|
||||
if let Err(inner_result_error) = inner_simple_decrypt(cmd_simple_decrypt) {
|
||||
CmdResult::fail(-1, &format!("{}", inner_result_error)).print_exit();
|
||||
CmdResult::fail(-1, &format!("{}", inner_result_error)).print_exit(direct_output);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -171,7 +192,7 @@ pub fn inner_simple_encrypt(cmd_simple_encrypt: CmdSimpleEncrypt) -> XResult<()>
|
||||
URL_SAFE_NO_PAD.encode(envelops_json.as_bytes())
|
||||
);
|
||||
|
||||
CmdResult::success(&simple_encrypt_result).print_exit();
|
||||
CmdResult::success(&simple_encrypt_result).print_exit(cmd_simple_encrypt.direct_output);
|
||||
}
|
||||
|
||||
pub fn inner_simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()> {
|
||||
@@ -197,7 +218,7 @@ pub fn inner_simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()>
|
||||
if value_parts[0] != SIMPLE_ENCRYPTION_HEADER {
|
||||
return simple_error!("bad value format: {}", value);
|
||||
}
|
||||
let envelopes_json = opt_result!(URL_SAFE_NO_PAD.decode(&value_parts[1]), "bad value format: {}");
|
||||
let envelopes_json = opt_result!(URL_SAFE_NO_PAD.decode(value_parts[1]), "bad value format: {}");
|
||||
let envelops: Vec<TinyEncryptEnvelop> = match serde_json::from_slice(&envelopes_json) {
|
||||
Err(_) => return simple_error!("bad value format: {}", value),
|
||||
Ok(value) => value,
|
||||
@@ -216,11 +237,15 @@ pub fn inner_simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()>
|
||||
return simple_error!("too many envelops: {:?}, len: {}", cmd_simple_decrypt.key_id, filter_envelops.len());
|
||||
}
|
||||
let value = try_decrypt_key(&config, filter_envelops[0], &pin, &slot, false)?;
|
||||
if cmd_simple_decrypt.direct_output && output_format == "plain" {
|
||||
io::stdout().write_all(&value).expect("unable to write to stdout");
|
||||
exit(0);
|
||||
}
|
||||
let value = match output_format {
|
||||
"plain" => opt_result!(String::from_utf8(value), "bad value encoding: {}"),
|
||||
"hex" => hex::encode(&value),
|
||||
"base64" => STANDARD.encode(&value),
|
||||
_ => return simple_error!("not supported output format: {}", output_format),
|
||||
};
|
||||
CmdResult::success(&value).print_exit();
|
||||
CmdResult::success(&value).print_exit(cmd_simple_decrypt.direct_output);
|
||||
}
|
||||
Reference in New Issue
Block a user