feat: update info
This commit is contained in:
@@ -11,7 +11,10 @@ use crate::file;
|
|||||||
pub fn decrypt(path: PathBuf, pin: &Option<String>) -> XResult<()> {
|
pub fn decrypt(path: PathBuf, pin: &Option<String>) -> XResult<()> {
|
||||||
let path_display = format!("{}", path.display());
|
let path_display = format!("{}", path.display());
|
||||||
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
|
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
|
||||||
let meta = opt_result!(file::read_tiny_encrypt_meta(&mut file_in), "Read file: {}, failed: {}", &path_display);
|
let mut meta = opt_result!(file::read_tiny_encrypt_meta(&mut file_in), "Read file: {}, failed: {}", &path_display);
|
||||||
|
meta.normalize();
|
||||||
|
|
||||||
|
debugging!("Found meta: {}", serde_json::to_string_pretty(&meta).unwrap());
|
||||||
|
|
||||||
let mut card = match get_card() {
|
let mut card = match get_card() {
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
|||||||
@@ -3,13 +3,14 @@ use std::fs::File;
|
|||||||
use std::ops::Add;
|
use std::ops::Add;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::time::{Duration, SystemTime};
|
use std::time::{Duration, SystemTime};
|
||||||
use rust_util::{information, opt_result, simple_error, success, XResult};
|
use rust_util::{iff, information, opt_result, simple_error, success, XResult};
|
||||||
use crate::file;
|
use crate::file;
|
||||||
|
|
||||||
pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
|
pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
|
||||||
let path_display = format!("{}", path.display());
|
let path_display = format!("{}", path.display());
|
||||||
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
|
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
|
||||||
let meta = opt_result!(file::read_tiny_encrypt_meta(&mut file_in), "Read file: {}, failed: {}", &path_display);
|
let mut meta = opt_result!(file::read_tiny_encrypt_meta(&mut file_in), "Read file: {}, failed: {}", &path_display);
|
||||||
|
meta.normalize();
|
||||||
|
|
||||||
if raw_meta {
|
if raw_meta {
|
||||||
success!("Meta data:\n{}", serde_json::to_string_pretty(&meta).expect("SHOULD NOT HAPPEN"));
|
success!("Meta data:\n{}", serde_json::to_string_pretty(&meta).expect("SHOULD NOT HAPPEN"));
|
||||||
@@ -29,11 +30,14 @@ pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
|
|||||||
infos.push(format!("{}: {}", header("Last modified"), fmt.format_local(from_unix_epoch(meta.file_last_modified))));
|
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!("{}: {}", header("Enc file created"), fmt.format_local(from_unix_epoch(meta.created))));
|
||||||
|
|
||||||
infos.push(format!("{}: KMS: {}, PGP: {}",
|
meta.envelops.as_ref().map(|envelops| envelops.iter().enumerate().for_each(|(i, envelop)| {
|
||||||
header("Envelops"),
|
infos.push(format!("{}: {}{}{}",
|
||||||
to_yes_or_no(&meta.envelop),
|
header(&format!("Envelop #{}", i + 1)),
|
||||||
to_yes_or_no(&meta.pgp_envelop)
|
envelop.r#type.to_uppercase(),
|
||||||
));
|
iff!(envelop.kid.is_empty(), "".into(), format!(", Kid: {}", envelop.kid)),
|
||||||
|
iff!(envelop.desc.is_none(), "".into(), format!(", Desc: {}", envelop.desc.as_ref().unwrap()))
|
||||||
|
));
|
||||||
|
}));
|
||||||
meta.pgp_fingerprint.map(|fingerprint| {
|
meta.pgp_fingerprint.map(|fingerprint| {
|
||||||
infos.push(format!("{}: {}", header("PGP fingerprint"), fingerprint));
|
infos.push(format!("{}: {}", header("PGP fingerprint"), fingerprint));
|
||||||
});
|
});
|
||||||
@@ -41,6 +45,13 @@ pub fn info(path: PathBuf, raw_meta: bool) -> XResult<()> {
|
|||||||
infos.push(format!("{}: {}", header("Comment"), comment));
|
infos.push(format!("{}: {}", header("Comment"), comment));
|
||||||
});
|
});
|
||||||
infos.push(format!("{}: {}", header("Encrypted comment"), to_yes_or_no(&meta.encrypted_comment)));
|
infos.push(format!("{}: {}", header("Encrypted comment"), to_yes_or_no(&meta.encrypted_comment)));
|
||||||
|
infos.push(format!("{}: {}", header("Encrypted meta"), to_yes_or_no(&meta.encrypted_meta)));
|
||||||
|
let encryption_algorithm = if let Some(encryption_algorithm) = &meta.encryption_algorithm {
|
||||||
|
encryption_algorithm.to_string()
|
||||||
|
} else {
|
||||||
|
"AES/GCM (default)".to_string()
|
||||||
|
};
|
||||||
|
infos.push(format!("{}: {}", header("Encryption algorithm"), encryption_algorithm));
|
||||||
|
|
||||||
success!("{}\n", infos.join("\n"));
|
success!("{}\n", infos.join("\n"));
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -51,7 +62,7 @@ fn from_unix_epoch(t: u64) -> SystemTime {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn header(h: &str) -> String {
|
fn header(h: &str) -> String {
|
||||||
let width = 18;
|
let width = 21;
|
||||||
h.to_string() + ".".repeat(max(width - h.len(), 0)).as_str()
|
h.to_string() + ".".repeat(max(width - h.len(), 0)).as_str()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
src/config.rs
Normal file
13
src/config.rs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
|
#[serde(rename_all = "camelCase")]
|
||||||
|
pub struct TinyEncryptConfig {
|
||||||
|
// card cli is not used by tiny-encrypt-rs
|
||||||
|
pub card_cli: String,
|
||||||
|
pub default_key_name: String,
|
||||||
|
pub local_private_key_pem_challenge: String,
|
||||||
|
pub local_private_key_pem_encrypted: String,
|
||||||
|
pub local_public_key_pem: String,
|
||||||
|
pub pgp_encrypt_public_key_pem: Option<String>,
|
||||||
|
}
|
||||||
@@ -5,6 +5,8 @@ use std::path::PathBuf;
|
|||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use rust_util::{information, XResult};
|
use rust_util::{information, XResult};
|
||||||
|
|
||||||
|
mod util;
|
||||||
|
mod config;
|
||||||
mod spec;
|
mod spec;
|
||||||
mod crypto;
|
mod crypto;
|
||||||
mod file;
|
mod file;
|
||||||
|
|||||||
64
src/spec.rs
64
src/spec.rs
@@ -2,18 +2,6 @@ use serde::{Deserialize, Serialize};
|
|||||||
|
|
||||||
pub const TINY_ENCRYPT_VERSION: &'static str = "1.0";
|
pub const TINY_ENCRYPT_VERSION: &'static str = "1.0";
|
||||||
|
|
||||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
||||||
#[serde(rename_all = "camelCase")]
|
|
||||||
pub struct TinyEncryptConfig {
|
|
||||||
// card cli is not used by tiny-encrypt-rs
|
|
||||||
pub card_cli: String,
|
|
||||||
pub default_key_name: String,
|
|
||||||
pub local_private_key_pem_challenge: String,
|
|
||||||
pub local_private_key_pem_encrypted: String,
|
|
||||||
pub local_public_key_pem: String,
|
|
||||||
pub pgp_encrypt_public_key_pem: Option<String>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Specification: [Tiny Encrypt Spec V1.1](https://git.hatter.ink/hatter/tiny-encrypt-java/src/branch/master/TinyEncryptSpecV1.1.md)
|
/// 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)]
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
@@ -33,7 +21,7 @@ pub struct TinyEncryptMeta {
|
|||||||
pub ecdh_point: Option<String>,
|
pub ecdh_point: Option<String>,
|
||||||
pub envelop: Option<String>,
|
pub envelop: Option<String>,
|
||||||
// ---------------------------------------
|
// ---------------------------------------
|
||||||
pub envelops: Vec<TinyEncryptEnvelop>,
|
pub envelops: Option<Vec<TinyEncryptEnvelop>>,
|
||||||
pub encryption_algorithm: Option<String>,
|
pub encryption_algorithm: Option<String>,
|
||||||
pub nonce: String,
|
pub nonce: String,
|
||||||
pub file_length: u64,
|
pub file_length: u64,
|
||||||
@@ -60,6 +48,9 @@ pub enum TinyEncryptEnvelopType {
|
|||||||
|
|
||||||
impl TinyEncryptMeta {
|
impl TinyEncryptMeta {
|
||||||
pub fn normalize(&mut self) {
|
pub fn normalize(&mut self) {
|
||||||
|
if self.envelops.is_none() {
|
||||||
|
self.envelops = Some(vec![]);
|
||||||
|
}
|
||||||
self.normalize_ppg_envelop();
|
self.normalize_ppg_envelop();
|
||||||
self.normalize_age_envelop();
|
self.normalize_age_envelop();
|
||||||
self.normalize_ecdh_envelop();
|
self.normalize_ecdh_envelop();
|
||||||
@@ -67,8 +58,9 @@ impl TinyEncryptMeta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn normalize_ppg_envelop(&mut self) {
|
fn normalize_ppg_envelop(&mut self) {
|
||||||
if let (Some(pgp_envelop), Some(pgp_fingerprint)) = (&self.pgp_envelop, &self.pgp_fingerprint) {
|
if let (Some(pgp_envelop), Some(pgp_fingerprint), Some(envelops))
|
||||||
self.envelops.push(TinyEncryptEnvelop {
|
= (&self.pgp_envelop, &self.pgp_fingerprint, &mut self.envelops) {
|
||||||
|
envelops.push(TinyEncryptEnvelop {
|
||||||
r#type: "pgp".to_string(),
|
r#type: "pgp".to_string(),
|
||||||
kid: pgp_fingerprint.into(),
|
kid: pgp_fingerprint.into(),
|
||||||
desc: None,
|
desc: None,
|
||||||
@@ -80,8 +72,9 @@ impl TinyEncryptMeta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn normalize_age_envelop(&mut self) {
|
fn normalize_age_envelop(&mut self) {
|
||||||
if let (Some(age_envelop), Some(age_recipient)) = (&self.age_envelop, &self.age_recipient) {
|
if let (Some(age_envelop), Some(age_recipient), Some(envelops))
|
||||||
self.envelops.push(TinyEncryptEnvelop {
|
= (&self.age_envelop, &self.age_recipient, &mut self.envelops) {
|
||||||
|
envelops.push(TinyEncryptEnvelop {
|
||||||
r#type: "age".to_string(),
|
r#type: "age".to_string(),
|
||||||
kid: age_recipient.into(),
|
kid: age_recipient.into(),
|
||||||
desc: None,
|
desc: None,
|
||||||
@@ -93,8 +86,9 @@ impl TinyEncryptMeta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn normalize_ecdh_envelop(&mut self) {
|
fn normalize_ecdh_envelop(&mut self) {
|
||||||
if let (Some(ecdh_envelop), Some(ecdh_point)) = (&self.ecdh_envelop, &self.ecdh_point) {
|
if let (Some(ecdh_envelop), Some(ecdh_point), Some(envelops))
|
||||||
self.envelops.push(TinyEncryptEnvelop {
|
= (&self.ecdh_envelop, &self.ecdh_point, &mut self.envelops) {
|
||||||
|
envelops.push(TinyEncryptEnvelop {
|
||||||
r#type: "ecdh".to_string(),
|
r#type: "ecdh".to_string(),
|
||||||
kid: ecdh_point.into(),
|
kid: ecdh_point.into(),
|
||||||
desc: None,
|
desc: None,
|
||||||
@@ -106,10 +100,10 @@ impl TinyEncryptMeta {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn normalize_envelop(&mut self) {
|
fn normalize_envelop(&mut self) {
|
||||||
if let Some(envelop) = &self.envelop {
|
if let (Some(envelop), Some(envelops)) = (&self.envelop, &mut self.envelops) {
|
||||||
self.envelops.push(TinyEncryptEnvelop {
|
envelops.push(TinyEncryptEnvelop {
|
||||||
r#type: "kms".to_string(),
|
r#type: "kms".to_string(),
|
||||||
kid: "#default".into(),
|
kid: "".into(),
|
||||||
desc: None,
|
desc: None,
|
||||||
encrypted_key: envelop.into(),
|
encrypted_key: envelop.into(),
|
||||||
});
|
});
|
||||||
@@ -117,29 +111,3 @@ impl TinyEncryptMeta {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_user_agent() -> String {
|
|
||||||
format!("TinyEncrypt-rs v{}@{}", env!("CARGO_PKG_VERSION"),
|
|
||||||
if cfg!(target_os = "macos") {
|
|
||||||
"MacOS"
|
|
||||||
} else if cfg!(target_os = "ios") {
|
|
||||||
"iOS"
|
|
||||||
} else if cfg!(target_os = "android") {
|
|
||||||
"Android"
|
|
||||||
} else if cfg!(target_os = "windows") {
|
|
||||||
"Windows"
|
|
||||||
} else if cfg!(target_os = "linux") {
|
|
||||||
"Linux"
|
|
||||||
} else if cfg!(target_os = "freebsd") {
|
|
||||||
"FreeBSD"
|
|
||||||
} else if cfg!(target_os = "dragonfly") {
|
|
||||||
"Dragonfly"
|
|
||||||
} else if cfg!(target_os = "openbsd") {
|
|
||||||
"OpenBSD"
|
|
||||||
} else if cfg!(target_os = "netbsd") {
|
|
||||||
"NetBSD"
|
|
||||||
} else {
|
|
||||||
panic!("Unsupported OS!");
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
26
src/util.rs
Normal file
26
src/util.rs
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
|
||||||
|
pub fn get_user_agent() -> String {
|
||||||
|
format!("TinyEncrypt-rs v{}@{}", env!("CARGO_PKG_VERSION"),
|
||||||
|
if cfg!(target_os = "macos") {
|
||||||
|
"MacOS"
|
||||||
|
} else if cfg!(target_os = "ios") {
|
||||||
|
"iOS"
|
||||||
|
} else if cfg!(target_os = "android") {
|
||||||
|
"Android"
|
||||||
|
} else if cfg!(target_os = "windows") {
|
||||||
|
"Windows"
|
||||||
|
} else if cfg!(target_os = "linux") {
|
||||||
|
"Linux"
|
||||||
|
} else if cfg!(target_os = "freebsd") {
|
||||||
|
"FreeBSD"
|
||||||
|
} else if cfg!(target_os = "dragonfly") {
|
||||||
|
"Dragonfly"
|
||||||
|
} else if cfg!(target_os = "openbsd") {
|
||||||
|
"OpenBSD"
|
||||||
|
} else if cfg!(target_os = "netbsd") {
|
||||||
|
"NetBSD"
|
||||||
|
} else {
|
||||||
|
panic!("Unsupported OS!");
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user