feat: v0.4.4, add radio outputs when compressed

This commit is contained in:
2023-10-19 21:55:19 +08:00
parent aa6c4f325d
commit 94a6cf18b3
5 changed files with 30 additions and 13 deletions

10
Cargo.lock generated
View File

@@ -1400,9 +1400,9 @@ dependencies = [
[[package]]
name = "rustix"
version = "0.38.19"
version = "0.38.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "745ecfa778e66b2b63c88a61cb36e0eea109e803b0b86bf9879fbc77c70e86ed"
checksum = "67ce50cb2e16c2903e30d1cbccfd8387a74b9d4c938b6a4c5ec6cc7556f7a8a0"
dependencies = [
"bitflags 2.4.1",
"errno",
@@ -1713,7 +1713,7 @@ dependencies = [
[[package]]
name = "tiny-encrypt"
version = "0.4.3"
version = "0.4.4"
dependencies = [
"aes-gcm-stream",
"base64",
@@ -1802,9 +1802,9 @@ checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "uuid"
version = "1.4.1"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d"
checksum = "88ad59a7560b41a70d191093a945f0b87bc1deeda46fb237479708a1d6b6cdfc"
dependencies = [
"getrandom",
]

View File

@@ -1,9 +1,10 @@
[package]
name = "tiny-encrypt"
version = "0.4.3"
version = "0.4.4"
edition = "2021"
license = "MIT"
description = "A simple and tiny file encrypt tool"
repository = "https://git.hatter.ink/hatter/tiny-encrypt-rs"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -44,16 +44,16 @@ pub struct CmdDecrypt {
#[arg(long, short = 'R')]
pub remove_file: bool,
/// Skip decrypt file
#[arg(long)]
#[arg(long, short = 'S')]
pub skip_decrypt_file: bool,
/// Direct print to the console, file must less than 10K
#[arg(long)]
#[arg(long, short = 'P')]
pub direct_print: bool,
/// Digest file
#[arg(long)]
#[arg(long, short = 'D')]
pub digest_file: bool,
/// Digest algorithm (sha1, sha256[default], sha384, sha512 ...)
#[arg(long)]
#[arg(long, short = 'A')]
pub digest_algorithm: Option<String>,
}

View File

@@ -200,6 +200,7 @@ fn process_compatible_with_1_0(cmd_encrypt: &CmdEncrypt, mut encrypt_meta: TinyE
fn encrypt_file(file_in: &mut File, file_len: u64, file_out: &mut File,
key: &[u8], nonce: &[u8], compress: bool, compress_level: &Option<u32>) -> XResult<u64> {
let mut total_len = 0_u64;
let mut write_len = 0_u64;
let mut buffer = [0u8; 1024 * 8];
let key = opt_result!(key.try_into(), "Key is not 32 bytes: {}");
let mut gz_encoder = match compress_level {
@@ -216,21 +217,28 @@ fn encrypt_file(file_in: &mut File, file_len: u64, file_out: &mut File,
loop {
let len = opt_result!(file_in.read(&mut buffer), "Read file failed: {}");
if len == 0 {
let last_block = if compress {
let last_block_and_tag = if compress {
let last_compressed_buffer = opt_result!(gz_encoder.finalize(), "Decompress file failed: {}");
let mut encrypted_block = encryptor.update(&last_compressed_buffer);
let (last_block, tag) = encryptor.finalize();
write_len += encrypted_block.len() as u64;
write_len += last_block.len() as u64;
encrypted_block.extend_from_slice(&last_block);
encrypted_block.extend_from_slice(&tag);
encrypted_block
} else {
let (mut last_block, tag) = encryptor.finalize();
write_len += last_block.len() as u64;
last_block.extend_from_slice(&tag);
last_block
};
opt_result!(file_out.write_all(&last_block), "Write file failed: {}");
debugging!("Encrypt finished, total bytes: {}", total_len);
opt_result!(file_out.write_all(&last_block_and_tag), "Write file failed: {}");
progress.finish();
debugging!("Encrypt finished, total bytes: {}", total_len);
if compress {
information!("File is compressed: {} byte(s) -> {} byte(s), ratio: {}%",
total_len, write_len, util::ratio(write_len, total_len));
}
break;
} else {
total_len += len as u64;
@@ -240,6 +248,7 @@ fn encrypt_file(file_in: &mut File, file_len: u64, file_out: &mut File,
} else {
encryptor.update(&buffer[0..len])
};
write_len += encrypted.len() as u64;
opt_result!(file_out.write_all(&encrypted), "Write file failed: {}");
progress.position(total_len);
}

View File

@@ -170,3 +170,10 @@ pub fn read_line(ln: &str) {
let _ = io::stdin().read_line(&mut buff).expect("Read line from stdin");
}
pub fn ratio(numerator: u64, denominator: u64) -> String {
if denominator == 0 {
return "".to_string();
}
let r = (numerator * 10000) / denominator;
format!("{:.2}", r as f64 / 100f64)
}