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]]
|
[[package]]
|
||||||
name = "tiny-encrypt"
|
name = "tiny-encrypt"
|
||||||
version = "1.8.0"
|
version = "1.8.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"aes-gcm-stream",
|
"aes-gcm-stream",
|
||||||
"base64 0.22.1",
|
"base64 0.22.1",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "tiny-encrypt"
|
name = "tiny-encrypt"
|
||||||
version = "1.8.0"
|
version = "1.8.1"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "A simple and tiny file encrypt tool"
|
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."));
|
command.arg(temp_file.to_str().expect("Get temp file path failed."));
|
||||||
if secure_editor {
|
if secure_editor {
|
||||||
command.arg("aes-256-gcm");
|
command.arg("aes-256-gcm");
|
||||||
command.arg(&hex::encode(&temp_encryption_key_nonce.0));
|
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.1));
|
||||||
if readonly { command.env("READONLY", "true"); }
|
if readonly { command.env("READONLY", "true"); }
|
||||||
}
|
}
|
||||||
debugging!("Run cmd: {:?}", command);
|
debugging!("Run cmd: {:?}", command);
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ use base64::Engine;
|
|||||||
use clap::Args;
|
use clap::Args;
|
||||||
use rust_util::{debugging, opt_result, simple_error, XResult};
|
use rust_util::{debugging, opt_result, simple_error, XResult};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
|
|
||||||
// Reference: https://git.hatter.ink/hatter/tiny-encrypt-rs/issues/3
|
// Reference: https://git.hatter.ink/hatter/tiny-encrypt-rs/issues/3
|
||||||
@@ -39,6 +41,10 @@ pub struct CmdSimpleEncrypt {
|
|||||||
/// Encrypt value in hex
|
/// Encrypt value in hex
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub value_hex: Option<String>,
|
pub value_hex: Option<String>,
|
||||||
|
|
||||||
|
/// Direct output result value
|
||||||
|
#[arg(long)]
|
||||||
|
pub direct_output: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Args)]
|
#[derive(Debug, Args)]
|
||||||
@@ -66,6 +72,10 @@ pub struct CmdSimpleDecrypt {
|
|||||||
/// Decrypt result output format (plain, hex, bse64)
|
/// Decrypt result output format (plain, hex, bse64)
|
||||||
#[arg(long, short = 'o')]
|
#[arg(long, short = 'o')]
|
||||||
pub output_format: Option<String>,
|
pub output_format: Option<String>,
|
||||||
|
|
||||||
|
/// Direct output result value
|
||||||
|
#[arg(long)]
|
||||||
|
pub direct_output: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CmdSimpleEncrypt {
|
impl CmdSimpleEncrypt {
|
||||||
@@ -121,23 +131,34 @@ impl CmdResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn print_exit(&self) -> ! {
|
pub fn print_exit(&self, direct_output_value: bool) -> ! {
|
||||||
let result = serde_json::to_string_pretty(self).unwrap();
|
// TODO direct_output_value
|
||||||
println!("{}", result);
|
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)
|
exit(self.code)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn simple_encrypt(cmd_simple_encrypt: CmdSimpleEncrypt) -> XResult<()> {
|
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) {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn simple_decrypt(cmd_simple_decrypt: CmdSimpleDecrypt) -> XResult<()> {
|
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) {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -171,7 +192,7 @@ pub fn inner_simple_encrypt(cmd_simple_encrypt: CmdSimpleEncrypt) -> XResult<()>
|
|||||||
URL_SAFE_NO_PAD.encode(envelops_json.as_bytes())
|
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<()> {
|
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 {
|
if value_parts[0] != SIMPLE_ENCRYPTION_HEADER {
|
||||||
return simple_error!("bad value format: {}", value);
|
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) {
|
let envelops: Vec<TinyEncryptEnvelop> = match serde_json::from_slice(&envelopes_json) {
|
||||||
Err(_) => return simple_error!("bad value format: {}", value),
|
Err(_) => return simple_error!("bad value format: {}", value),
|
||||||
Ok(value) => 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());
|
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)?;
|
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 {
|
let value = match output_format {
|
||||||
"plain" => opt_result!(String::from_utf8(value), "bad value encoding: {}"),
|
"plain" => opt_result!(String::from_utf8(value), "bad value encoding: {}"),
|
||||||
"hex" => hex::encode(&value),
|
"hex" => hex::encode(&value),
|
||||||
"base64" => STANDARD.encode(&value),
|
"base64" => STANDARD.encode(&value),
|
||||||
_ => return simple_error!("not supported output format: {}", output_format),
|
_ => 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