feat: update tiny-encrypt info

This commit is contained in:
2023-09-06 01:13:15 +08:00
parent d96ebdd5ba
commit 34fd7d0e47
4 changed files with 44 additions and 26 deletions

4
Cargo.lock generated
View File

@@ -1421,9 +1421,9 @@ dependencies = [
[[package]]
name = "simpledateformat"
version = "0.1.3"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd2436140481bbe945c4d6deab521cf1ac52d73766e3c5d28d2c65e2635ae115"
checksum = "d99ace355f41f2391428701c164540ab9b0b644c0aa935f3eefe773dc0072a3e"
dependencies = [
"chrono",
"quick-error",

View File

@@ -20,7 +20,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"
simpledateformat = "0.1.4"
yubico_manager = "0.9.0"
[profile.release]

View File

@@ -3,7 +3,11 @@ use std::fs::File;
use std::ops::Add;
use std::path::PathBuf;
use std::time::{Duration, SystemTime};
use rust_util::{iff, information, opt_result, simple_error, success, XResult};
use rust_util::{iff, opt_result, success, XResult};
use rust_util::util_time::get_current_millis;
use simpledateformat::format_human2;
use crate::file;
pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
@@ -20,15 +24,24 @@ pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
let mut infos = vec![];
infos.push("Tiny Encrypt File Info".to_string());
let compressed = if meta.compress { " [compressed]" } else { "" };
infos.push(format!("{}: {}{}", header("File"), path_display, compressed));
infos.push(format!("{}: {}{}", header("File name"), 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)
header("File summary"), meta.version, meta.user_agent)
);
let now_millis = get_current_millis() as u64;
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!("{}: {}, {} ago",
header("Last modified"),
fmt.format_local(from_unix_epoch(meta.file_last_modified)),
format_human2(Duration::from_millis(now_millis - meta.file_last_modified))
));
infos.push(format!("{}: {}, {} ago",
header("Created"),
fmt.format_local(from_unix_epoch(meta.created)),
format_human2(Duration::from_millis(now_millis - meta.created))
));
meta.envelops.as_ref().map(|envelops| envelops.iter().enumerate().for_each(|(i, envelop)| {
infos.push(format!("{}: {}{}{}",

View File

@@ -2,6 +2,11 @@ use serde::{Deserialize, Serialize};
pub const TINY_ENCRYPT_VERSION: &'static str = "1.0";
pub const ENVELOP_TYPE_KMS: &'static str = "kms";
pub const ENVELOP_TYPE_PGP: &'static str = "pgp";
pub const ENVELOP_TYPE_AGE: &'static str = "age";
pub const ENVELOP_TYPE_ECDH: &'static str = "ecdh";
/// Specification: [Tiny Encrypt Spec V1.1](https://git.hatter.ink/hatter/tiny-encrypt-java/src/branch/master/TinyEncryptSpecV1.1.md)
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
@@ -51,17 +56,29 @@ impl TinyEncryptMeta {
if self.envelops.is_none() {
self.envelops = Some(vec![]);
}
self.normalize_ppg_envelop();
self.normalize_envelop();
self.normalize_pgp_envelop();
self.normalize_age_envelop();
self.normalize_ecdh_envelop();
self.normalize_envelop();
}
fn normalize_ppg_envelop(&mut self) {
fn normalize_envelop(&mut self) {
if let (Some(envelop), Some(envelops)) = (&self.envelop, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: ENVELOP_TYPE_KMS.into(),
kid: "".into(),
desc: None,
encrypted_key: envelop.into(),
});
self.envelop = None;
}
}
fn normalize_pgp_envelop(&mut self) {
if let (Some(pgp_envelop), Some(pgp_fingerprint), Some(envelops))
= (&self.pgp_envelop, &self.pgp_fingerprint, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: "pgp".to_string(),
r#type: ENVELOP_TYPE_PGP.into(),
kid: pgp_fingerprint.into(),
desc: None,
encrypted_key: pgp_envelop.into(),
@@ -75,7 +92,7 @@ impl TinyEncryptMeta {
if let (Some(age_envelop), Some(age_recipient), Some(envelops))
= (&self.age_envelop, &self.age_recipient, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: "age".to_string(),
r#type: ENVELOP_TYPE_AGE.into(),
kid: age_recipient.into(),
desc: None,
encrypted_key: age_envelop.into(),
@@ -89,7 +106,7 @@ impl TinyEncryptMeta {
if let (Some(ecdh_envelop), Some(ecdh_point), Some(envelops))
= (&self.ecdh_envelop, &self.ecdh_point, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: "ecdh".to_string(),
r#type: ENVELOP_TYPE_ECDH.into(),
kid: ecdh_point.into(),
desc: None,
encrypted_key: ecdh_envelop.into(),
@@ -98,16 +115,4 @@ impl TinyEncryptMeta {
self.ecdh_point = None;
}
}
fn normalize_envelop(&mut self) {
if let (Some(envelop), Some(envelops)) = (&self.envelop, &mut self.envelops) {
envelops.push(TinyEncryptEnvelop {
r#type: "kms".to_string(),
kid: "".into(),
desc: None,
encrypted_key: envelop.into(),
});
self.envelop = None;
}
}
}