From c873a52ac68cf06ab2d698a0fdd9b0fc63d6e115 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Thu, 12 Oct 2023 23:42:03 +0800 Subject: [PATCH] feat: 0.3.1, update encrypts --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/cmd_encrypt.rs | 23 ++++++++--------------- src/file.rs | 27 ++++++++++++++++----------- 4 files changed, 26 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b669a4b..47c537e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2117,7 +2117,7 @@ dependencies = [ [[package]] name = "tiny-encrypt" -version = "0.3.0" +version = "0.3.1" dependencies = [ "aes-gcm-stream", "base64", diff --git a/Cargo.toml b/Cargo.toml index 05f6341..ca64ca9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tiny-encrypt" -version = "0.3.0" +version = "0.3.1" edition = "2021" license = "MIT" description = "A simple and tiny file encrypt tool" diff --git a/src/cmd_encrypt.rs b/src/cmd_encrypt.rs index 035e6c3..2e985eb 100644 --- a/src/cmd_encrypt.rs +++ b/src/cmd_encrypt.rs @@ -7,10 +7,10 @@ use std::time::Instant; use clap::Args; use flate2::Compression; use rsa::Pkcs1v15Encrypt; -use rust_util::{debugging, failure, iff, information, opt_result, simple_error, success, util_msg, warning, XResult}; +use rust_util::{debugging, failure, information, opt_result, simple_error, success, util_msg, warning, XResult}; use zeroize::Zeroize; -use crate::{compress, util, util_ecdh, util_p384, util_x25519}; +use crate::{file, util, util_ecdh, util_p384, util_x25519}; use crate::compress::GzStreamEncoder; use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop}; use crate::crypto_aes::{aes_gcm_encrypt, aes_gcm_encrypt_with_salt}; @@ -138,6 +138,10 @@ fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_en debugging!("Encrypted meta: {:?}", encrypt_meta); if cmd_encrypt.compatible_with_1_0 { + if !cmd_encrypt.disable_compress_meta { + return simple_error!("Compatible with 1.0 mode must turns --disable-compress-meta on."); + } + if let Some(envelops) = encrypt_meta.envelops { let mut filter_envelops = vec![]; for envelop in envelops { @@ -158,20 +162,9 @@ fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_en } } - let compress_meta = !cmd_encrypt.disable_compress_meta; - let mut file_out = File::create(&path_out)?; - let tag = iff!(compress_meta, util::TINY_ENC_COMPRESSED_MAGIC_TAG, util::TINY_ENC_MAGIC_TAG); - opt_result!(file_out.write_all(&tag.to_be_bytes()), "Write tag failed: {}"); - let mut encrypted_meta_bytes = opt_result!(serde_json::to_vec(&encrypt_meta), "Generate meta json bytes failed: {}"); - if compress_meta { - encrypted_meta_bytes = opt_result!( - compress::compress(Compression::default(), &encrypted_meta_bytes), "Compress encrypted meta failed: {}"); - } - let encrypted_meta_bytes_len = encrypted_meta_bytes.len() as u32; - debugging!("Encrypted meta len: {}", encrypted_meta_bytes_len); - opt_result!(file_out.write_all(&encrypted_meta_bytes_len.to_be_bytes()), "Write meta len failed: {}"); - opt_result!(file_out.write_all(&encrypted_meta_bytes), "Write meta failed: {}"); + let compress_meta = !cmd_encrypt.disable_compress_meta; + let _ = file::write_tiny_encrypt_meta(&mut file_out, &encrypt_meta, compress_meta)?; let start = Instant::now(); util_msg::print_lastline(&format!("Encrypting file: {} ...", path_display)); diff --git a/src/file.rs b/src/file.rs index 086da61..b286a2c 100644 --- a/src/file.rs +++ b/src/file.rs @@ -1,20 +1,25 @@ use std::io::{Read, Write}; -use rust_util::{debugging, opt_result, simple_error, XResult}; +use flate2::Compression; +use rust_util::{debugging, iff, opt_result, simple_error, XResult}; -use crate::spec::TinyEncryptMeta; use crate::{compress, util}; +use crate::spec::TinyEncryptMeta; -pub fn _write_tiny_encrypt_meta(w: &mut W, meta: &TinyEncryptMeta) -> XResult { - let meta_json = opt_result!( serde_json::to_string(meta), "Meta to JSON failed: {}"); - let meta_json_bytes = meta_json.as_bytes(); - let meta_json_bytes_len = meta_json_bytes.len(); +pub fn write_tiny_encrypt_meta(w: &mut W, meta: &TinyEncryptMeta, compress_meta: bool) -> XResult { + let tag = iff!(compress_meta, util::TINY_ENC_COMPRESSED_MAGIC_TAG, util::TINY_ENC_MAGIC_TAG); + opt_result!(w.write_all(&tag.to_be_bytes()), "Write tag failed: {}"); + let mut encrypted_meta_bytes = opt_result!(serde_json::to_vec(&meta), "Generate meta json bytes failed: {}"); + if compress_meta { + encrypted_meta_bytes = opt_result!( + compress::compress(Compression::default(), &encrypted_meta_bytes), "Compress encrypted meta failed: {}"); + } + let encrypted_meta_bytes_len = encrypted_meta_bytes.len() as u32; + debugging!("Encrypted meta len: {}", encrypted_meta_bytes_len); + opt_result!(w.write_all(&encrypted_meta_bytes_len.to_be_bytes()), "Write meta len failed: {}"); + opt_result!(w.write_all(&encrypted_meta_bytes), "Write meta failed: {}"); - opt_result!(w.write_all(&((0x01) as u16).to_be_bytes()), "Write tag failed: {}"); - opt_result!(w.write_all(&(meta_json_bytes_len as u32).to_be_bytes()), "Write length failed: {}"); - opt_result!(w.write_all(&meta_json_bytes), "Write meta failed: {}"); - - Ok(meta_json_bytes_len + 2 + 4) + Ok(encrypted_meta_bytes.len() + 2 + 4) } pub fn read_tiny_encrypt_meta_and_normalize(r: &mut R) -> XResult {