From 7a3cb8d243d5da48ecde6680473266b46f6af39c Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sat, 30 Sep 2023 20:27:04 +0800 Subject: [PATCH] feat: add comptible with 1.0 --- src/cmd_decrypt.rs | 2 +- src/cmd_encrypt.rs | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/cmd_decrypt.rs b/src/cmd_decrypt.rs index ba54a41..12b8787 100644 --- a/src/cmd_decrypt.rs +++ b/src/cmd_decrypt.rs @@ -29,7 +29,7 @@ pub struct CmdDecrypt { /// PIN #[arg(long, short = 'p')] pub pin: Option, - /// SLOT + /// Slot #[arg(long, short = 's')] pub slot: Option, } diff --git a/src/cmd_encrypt.rs b/src/cmd_encrypt.rs index d3ed187..3ccac26 100644 --- a/src/cmd_encrypt.rs +++ b/src/cmd_encrypt.rs @@ -22,17 +22,21 @@ use crate::wrap_key::{WrapKey, WrapKeyHeader}; pub struct CmdEncrypt { /// Files need to be decrypted pub paths: Vec, - // Comment + /// Comment #[arg(long, short = 'c')] pub comment: Option, - // Comment + /// Encrypted comment #[arg(long, short = 'C')] pub encrypted_comment: Option, - // Encryption profile + /// Encryption profile #[arg(long, short = 'p')] pub profile: Option, + /// Compress before encrypt #[arg(long, short = 'x')] pub compress: bool, + /// Compatible with 1.0 + #[arg(long, short = '1')] + pub compatible_with_1_0: bool, } pub fn encrypt(cmd_encrypt: CmdEncrypt) -> XResult<()> { @@ -78,9 +82,23 @@ fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_en compress: cmd_encrypt.compress, }; - let encrypt_meta = TinyEncryptMeta::new(&file_metadata, &enc_metadata, &nonce, envelops); + let mut encrypt_meta = TinyEncryptMeta::new(&file_metadata, &enc_metadata, &nonce, envelops); debugging!("Encrypted meta: {:?}", encrypt_meta); + if cmd_encrypt.compatible_with_1_0 { + if let Some(envelops) = &encrypt_meta.envelops { + for envelop in envelops { + if envelop.r#type == TinyEncryptEnvelopType::Pgp { + encrypt_meta.pgp_fingerprint = Some(format!("KID:{}", envelop.kid)); + encrypt_meta.pgp_envelop = Some(envelop.encrypted_key.clone()); + } + if envelop.r#type == TinyEncryptEnvelopType::Ecdh { + encrypt_meta.ecdh_point = Some(format!("KID:{}", envelop.kid)); + encrypt_meta.ecdh_envelop = Some(envelop.encrypted_key.clone()); + } + } + } + } let mut file_out = File::create(&path_out)?; opt_result!(file_out.write_all(&util::TINY_ENC_MAGIC_TAG.to_be_bytes()), "Write tag failed: {}");