From 9e367259087c12b4964b8a059a0dc4aba8b1f891 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 2 Dec 2023 12:20:28 +0800 Subject: [PATCH] feat: v0.9.0, support secure editor --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 12 ++++++++++++ src/cmd_decrypt.rs | 4 ++++ src/cmd_info.rs | 16 +++++++++++----- src/spec.rs | 2 ++ 6 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 517233a..053e403 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1657,7 +1657,7 @@ dependencies = [ [[package]] name = "tiny-encrypt" -version = "0.8.2" +version = "0.9.0" dependencies = [ "aes-gcm-stream", "base64", diff --git a/Cargo.toml b/Cargo.toml index a1a697f..76274ca 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tiny-encrypt" -version = "0.8.2" +version = "0.9.0" edition = "2021" license = "MIT" description = "A simple and tiny file encrypt tool" diff --git a/README.md b/README.md index f5871c3..98b3088 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,18 @@ Compile only encrypt: cargo build --release --no-default-features ``` +Edit encrypted file: +```shell +tiny-encrypt decrypt --edit-file sample.txt.tinyenc +``` +Read environment `EDITOR` or `SECURE_EDITOR` to edit file, `SECURE_EDITOR` write encrypted file to temp file. + +Secure editor command format: +```shell +$SECURE_EDITOR "aes-256-gcm" +``` + +
Encrypt config `~/.tinyencrypt/config-rs.json`: diff --git a/src/cmd_decrypt.rs b/src/cmd_decrypt.rs index 995e221..6fe4b76 100644 --- a/src/cmd_decrypt.rs +++ b/src/cmd_decrypt.rs @@ -218,6 +218,10 @@ pub fn decrypt_single(config: &Option, let mut meta = meta; meta.file_length = temp_file_content.len() as u64; meta.file_last_modified = util_time::get_current_millis() as u64; + match &mut meta.file_edit_count { + None => { meta.file_edit_count = Some(1); } + Some(file_edit_count) => { *file_edit_count += 1; } + } let mut file_out = File::create(path)?; let _ = util_enc_file::write_tiny_encrypt_meta(&mut file_out, &meta, is_meta_compressed)?; let compress_level = iff!(meta.compress, Some(Compression::default().level()), None); diff --git a/src/cmd_info.rs b/src/cmd_info.rs index 7a0fa11..b6fb63e 100644 --- a/src/cmd_info.rs +++ b/src/cmd_info.rs @@ -79,16 +79,22 @@ pub fn info_single(path: &PathBuf, cmd_info: &CmdInfo) -> XResult<()> { let now_millis = util_time::get_current_millis() as u64; let fmt = simpledateformat::fmt(DATE_TIME_FORMAT).unwrap(); - infos.push(format!("{}: {}, {} ago", - header("Last modified"), - fmt.format_local(SystemTime::from_millis(meta.file_last_modified)), - format_human2(Duration::from_millis(now_millis - meta.file_last_modified)) - )); infos.push(format!("{}: {}, {} ago", header("Created"), fmt.format_local(SystemTime::from_millis(meta.created)), format_human2(Duration::from_millis(now_millis - meta.created)) )); + infos.push(format!("{}: {}, {} ago", + header("Last modified"), + fmt.format_local(SystemTime::from_millis(meta.file_last_modified)), + format_human2(Duration::from_millis(now_millis - meta.file_last_modified)) + )); + if let Some(file_edit_count) = meta.file_edit_count { + infos.push(format!("{}: {} time(s)", + header("Edit count"), + file_edit_count + )); + } if let Some(envelops) = meta.envelops.as_ref() { envelops.iter().enumerate().for_each(|(i, envelop)| { diff --git a/src/spec.rs b/src/spec.rs index 4be367a..288c30d 100644 --- a/src/spec.rs +++ b/src/spec.rs @@ -48,6 +48,7 @@ pub struct TinyEncryptMeta { pub nonce: String, pub file_length: u64, pub file_last_modified: u64, + pub file_edit_count: Option, pub compress: bool, } @@ -161,6 +162,7 @@ impl TinyEncryptMeta { Ok(modified) => get_millis(&modified) as u64, Err(_) => 0, }, + file_edit_count: None, compress: enc_metadata.compress, } }