diff --git a/Cargo.lock b/Cargo.lock index 9b25c4e..d50de8b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1100,6 +1100,12 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "0.3.15" @@ -1404,6 +1410,16 @@ dependencies = [ "opaque-debug", ] +[[package]] +name = "simpledateformat" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd2436140481bbe945c4d6deab521cf1ac52d73766e3c5d28d2c65e2635ae115" +dependencies = [ + "chrono", + "quick-error", +] + [[package]] name = "slab" version = "0.4.7" @@ -1554,6 +1570,7 @@ version = "0.0.0" dependencies = [ "aes-gcm", "base64", + "chrono", "clap", "hex", "openpgp-card", @@ -1562,6 +1579,7 @@ dependencies = [ "rust_util", "serde", "serde_json", + "simpledateformat", "yubico_manager", ] diff --git a/Cargo.toml b/Cargo.toml index 8938518..a5b1520 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,6 +10,7 @@ description = "A simple and tiny file encrypt tool" [dependencies] aes-gcm = { version = "0.10.1", features = ["zeroize"] } base64 = "0.21.0" +chrono = "0.4.23" clap = { version = "4.1.4", features = ["derive"] } hex = "0.4.3" openpgp-card = "0.3.3" @@ -18,6 +19,7 @@ reqwest = { version = "0.11.14", features = ["blocking", "rustls", "rustls-tls"] rust_util = "0.6.41" serde = { version = "1.0.152", features = ["derive"] } serde_json = "1.0.93" +simpledateformat = "0.1.3" yubico_manager = "0.9.0" [profile.release] diff --git a/src/cmd_decrypt.rs b/src/cmd_decrypt.rs index 8d02252..da77edc 100644 --- a/src/cmd_decrypt.rs +++ b/src/cmd_decrypt.rs @@ -1,6 +1,5 @@ use std::path::PathBuf; use std::fs::File; -use std::io::Read; use base64::Engine; use base64::engine::general_purpose; use openpgp_card::crypto_data::Cryptogram; diff --git a/src/cmd_info.rs b/src/cmd_info.rs index 5d08b2c..4574d03 100644 --- a/src/cmd_info.rs +++ b/src/cmd_info.rs @@ -1,5 +1,8 @@ +use std::cmp::max; use std::fs::File; +use std::ops::Add; use std::path::PathBuf; +use std::time::{Duration, SystemTime}; use rust_util::{information, opt_result, simple_error, XResult}; use crate::file; @@ -10,24 +13,43 @@ pub fn info(path: PathBuf) -> XResult<()> { let mut infos = vec![]; infos.push("Tiny Encrypt File Info".to_string()); - infos.push(format!("File..............: {}", path_display)); - infos.push(format!("File size.........: {} bytes", meta.file_length)); - infos.push(format!("Enc file summary..: Version: {}, Agent: {}", meta.version, meta.user_agent)); - infos.push(format!("Last modified.....: {}", meta.file_last_modified)); - infos.push(format!("Enc file created..: {}", meta.created)); - infos.push(format!("Envelops..........: KMS: {}, PGP: {}", + let compressed = if meta.compress { " [compressed]" } else { "" }; + infos.push(format!("{}: {}{}", header("File"), path_display, compressed)); + infos.push(format!("{}: {} bytes", header("File size"), meta.file_length)); + infos.push(format!("{}: Version: {}, Agent: {}", + header("Enc file summary"), meta.version, meta.user_agent) + ); + + let fmt = simpledateformat::fmt("EEE MMM dd HH:MM:ss z yyyy").unwrap(); + infos.push(format!("{}: {}", header("Last modified"), fmt.format_local(from_unix_epoch(meta.file_last_modified)))); + infos.push(format!("{}: {}", header("Enc file created"), fmt.format_local(from_unix_epoch(meta.created)))); + + infos.push(format!("{}: KMS: {}, PGP: {}", + header("Envelops"), to_yes_or_no(&meta.envelop), to_yes_or_no(&meta.pgp_envelop) )); meta.pgp_fingerprint.map(|fingerprint| { - infos.push(format!("PGP fingerprint...: {}", fingerprint)); + infos.push(format!("{}: {}", header("PGP fingerprint"), fingerprint)); }); - infos.push(format!("Encrypted comment.: {}", to_yes_or_no(&meta.encrypted_comment))); + meta.comment.map(|comment| { + infos.push(format!("{}: {}", header("Comment"), comment)); + }); + infos.push(format!("{}: {}", header("Encrypted comment"), to_yes_or_no(&meta.encrypted_comment))); information!("{}\n", infos.join("\n")); Ok(()) } +fn from_unix_epoch(t: u64) -> SystemTime { + SystemTime::UNIX_EPOCH.add(Duration::from_millis(t)) +} + +fn header(h: &str) -> String { + let width = 18; + h.to_string() + ".".repeat(max(width - h.len(), 0)).as_str() +} + fn to_yes_or_no(opt: &Option) -> String { opt.as_ref().map(|_| "YES".to_string()).unwrap_or_else(|| "NO".to_string()) } \ No newline at end of file diff --git a/src/file.rs b/src/file.rs index 75b21e4..185fd1d 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,4 +1,3 @@ -use std::fs::File; use std::io::{Read, Write}; use rust_util::{opt_result, simple_error, XResult}; diff --git a/src/main.rs b/src/main.rs index 7188130..d447326 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ extern crate core; -use std::fs::File; use std::path::PathBuf; use clap::{Parser, Subcommand};