feat: v0.4.2, add --direct-print
This commit is contained in:
750
Cargo.lock
generated
750
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "tiny-encrypt"
|
||||
version = "0.4.1"
|
||||
version = "0.4.2"
|
||||
edition = "2021"
|
||||
license = "MIT"
|
||||
description = "A simple and tiny file encrypt tool"
|
||||
@@ -21,7 +21,7 @@ openpgp-card-pcsc = "0.3"
|
||||
p256 = { version = "0.13", features = ["pem", "ecdh", "pkcs8"] }
|
||||
p384 = { version = "0.13", features = ["pem", "ecdh", "pkcs8"] }
|
||||
rand = "0.8"
|
||||
reqwest = { version = "0.11", features = ["blocking", "rustls", "rustls-tls"] }
|
||||
# reqwest = { version = "0.11", features = ["blocking", "rustls", "rustls-tls"] }
|
||||
rpassword = "7.2"
|
||||
rsa = { version = "0.9", features = ["pem"] }
|
||||
rust_util = "0.6"
|
||||
|
||||
@@ -8,6 +8,7 @@ use tabled::settings::Style;
|
||||
|
||||
use crate::config::TinyEncryptConfig;
|
||||
use crate::consts::TINY_ENC_CONFIG_FILE;
|
||||
use crate::util_envelop;
|
||||
|
||||
#[derive(Tabled, Eq)]
|
||||
struct ConfigProfile {
|
||||
@@ -130,7 +131,10 @@ fn config_profiles(cmd_version: &CmdConfig, config: &TinyEncryptConfig) -> XResu
|
||||
let desc = envelop.desc.as_ref()
|
||||
.map(|desc| format!(", Desc: {}", desc))
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
ks.push(format!("{}, {}{}", envelop.r#type.get_name(), kid, desc));
|
||||
ks.push(format!(
|
||||
"{}, {}{}",
|
||||
util_envelop::with_width_type(envelop.r#type.get_name()), kid, desc
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +44,9 @@ pub struct CmdDecrypt {
|
||||
/// Skip decrypt file
|
||||
#[arg(long)]
|
||||
pub skip_decrypt_file: bool,
|
||||
/// Direct print to the console, file must less than 10K
|
||||
#[arg(long)]
|
||||
pub direct_print: bool,
|
||||
}
|
||||
|
||||
pub fn decrypt(cmd_decrypt: CmdDecrypt) -> XResult<()> {
|
||||
@@ -94,11 +97,13 @@ pub fn decrypt_single(config: &Option<TinyEncryptConfig>,
|
||||
util::require_tiny_enc_file_and_exists(path)?;
|
||||
|
||||
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
|
||||
let meta = opt_result!(util_enc_file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display);
|
||||
let meta = opt_result!(
|
||||
util_enc_file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display);
|
||||
debugging!("Found meta: {}", serde_json::to_string_pretty(&meta).unwrap());
|
||||
|
||||
let do_write_file_out = cmd_decrypt.skip_decrypt_file || cmd_decrypt.direct_print;
|
||||
let path_out = &path_display[0..path_display.len() - TINY_ENC_FILE_EXT.len()];
|
||||
util::require_file_not_exists(path_out)?;
|
||||
if !do_write_file_out { util::require_file_not_exists(path_out)?; }
|
||||
|
||||
let selected_envelop = select_envelop(&meta, config)?;
|
||||
|
||||
@@ -117,12 +122,27 @@ pub fn decrypt_single(config: &Option<TinyEncryptConfig>,
|
||||
let compressed_desc = iff!(meta.compress, " [compressed]", "");
|
||||
let start = Instant::now();
|
||||
|
||||
let mut file_out = File::create(path_out)?;
|
||||
let _ = decrypt_file(
|
||||
&mut file_in, meta.file_length, &mut file_out,
|
||||
&key, &nonce, meta.compress,
|
||||
)?;
|
||||
drop(file_out);
|
||||
if cmd_decrypt.direct_print {
|
||||
if meta.file_length > 10 * 1024 {
|
||||
return simple_error!("File too large(more than 10K) cannot direct print on console.");
|
||||
}
|
||||
let mut output: Vec<u8> = Vec::with_capacity(10 * 1024);
|
||||
let _ = decrypt_file(
|
||||
&mut file_in, meta.file_length, &mut output,
|
||||
&key, &nonce, meta.compress,
|
||||
)?;
|
||||
match String::from_utf8(output) {
|
||||
Err(_) => return simple_error!("File is not UTF-8 content."),
|
||||
Ok(output) => println!(">>>>> BEGIN CONTENT >>>>>\n{}\n<<<<< END CONTENT <<<<<", &output),
|
||||
}
|
||||
} else {
|
||||
let mut file_out = File::create(path_out)?;
|
||||
let _ = decrypt_file(
|
||||
&mut file_in, meta.file_length, &mut file_out,
|
||||
&key, &nonce, meta.compress,
|
||||
)?;
|
||||
drop(file_out);
|
||||
}
|
||||
|
||||
util_file::update_out_file_time(enc_meta, path_out);
|
||||
let encrypt_duration = start.elapsed();
|
||||
@@ -136,7 +156,7 @@ pub fn decrypt_single(config: &Option<TinyEncryptConfig>,
|
||||
Ok(meta.file_length)
|
||||
}
|
||||
|
||||
fn decrypt_file(file_in: &mut File, file_len: u64, file_out: &mut File,
|
||||
fn decrypt_file(file_in: &mut File, file_len: u64, file_out: &mut impl Write,
|
||||
key: &[u8], nonce: &[u8], compress: bool) -> XResult<u64> {
|
||||
let mut total_len = 0_u64;
|
||||
let mut buffer = [0u8; 1024 * 8];
|
||||
@@ -241,7 +261,8 @@ fn try_decrypt_key_ecdh(config: &Option<TinyEncryptConfig>,
|
||||
}
|
||||
let e_pub_key = &wrap_key.header.e_pub_key;
|
||||
let e_pub_key_bytes = opt_result!(util::decode_base64_url_no_pad(e_pub_key), "Invalid envelop: {}");
|
||||
let (_, subject_public_key_info) = opt_result!(SubjectPublicKeyInfo::from_der(&e_pub_key_bytes), "Invalid envelop: {}");
|
||||
let (_, subject_public_key_info) = opt_result!(
|
||||
SubjectPublicKeyInfo::from_der(&e_pub_key_bytes), "Invalid envelop: {}");
|
||||
|
||||
let slot = util_piv::read_piv_slot(config, &envelop.kid, slot)?;
|
||||
let pin = util::read_pin(pin);
|
||||
|
||||
@@ -6,7 +6,7 @@ pub struct CmdVersion {}
|
||||
|
||||
pub fn version(_cmd_version: CmdVersion) -> XResult<()> {
|
||||
println!(
|
||||
"{} - {}\n{}\n",
|
||||
"{} - v{}\n{}\n",
|
||||
env!("CARGO_PKG_NAME"),
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
env!("CARGO_PKG_DESCRIPTION")
|
||||
|
||||
@@ -12,7 +12,7 @@ pub fn format_envelop(envelop: &TinyEncryptEnvelop, config: &Option<TinyEncryptC
|
||||
let desc = envelop_desc.as_ref()
|
||||
.map(|desc| format!(", Desc: {}", desc))
|
||||
.unwrap_or_else(|| "".to_string());
|
||||
format!("{}{}{}", with_with(&envelop.r#type.get_upper_name(), 10), envelop_kid, desc)
|
||||
format!("{}{}{}", with_width_type(&envelop.r#type.get_upper_name()), envelop_kid, desc)
|
||||
}
|
||||
|
||||
fn get_envelop_desc(envelop: &TinyEncryptEnvelop, config_envelop: &Option<&TinyEncryptConfigEnvelop>) -> Option<String> {
|
||||
@@ -25,6 +25,10 @@ fn get_envelop_desc(envelop: &TinyEncryptEnvelop, config_envelop: &Option<&TinyE
|
||||
None
|
||||
}
|
||||
|
||||
fn with_with(s: &str, width: usize) -> String {
|
||||
pub fn with_width_type(s: &str) -> String {
|
||||
with_width(s, 10)
|
||||
}
|
||||
|
||||
pub fn with_width(s: &str, width: usize) -> String {
|
||||
iff!(s.len() < width, format!("{}{}", s, " ".repeat(width - s.len())), s.to_string())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user