feat: v0.4.2, add --direct-print
This commit is contained in:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user