feat: supports -R

This commit is contained in:
2023-09-30 21:40:27 +08:00
parent b8ef9b5239
commit 71ebe68a86
2 changed files with 32 additions and 10 deletions

View File

@@ -6,14 +6,14 @@ use std::time::Instant;
use clap::Args;
use rsa::Pkcs1v15Encrypt;
use rust_util::{debugging, failure, opt_result, simple_error, success, XResult};
use rust_util::{debugging, failure, information, opt_result, simple_error, success, warning, XResult};
use crate::util;
use crate::compress::GzStreamEncoder;
use crate::config::{TinyEncryptConfig, TinyEncryptConfigEnvelop};
use crate::crypto_aes::aes_gcm_encrypt;
use crate::crypto_rsa::parse_spki;
use crate::spec::{EncMetadata, TINY_ENCRYPT_VERSION_10, TinyEncryptEnvelop, TinyEncryptEnvelopType, TinyEncryptMeta};
use crate::util;
use crate::util::{ENC_AES256_GCM_P256, encode_base64, encode_base64_url_no_pad, make_key256_and_nonce, simple_kdf, TINY_ENC_CONFIG_FILE, zeroize};
use crate::util_ecdh::compute_shared_secret;
use crate::wrap_key::{WrapKey, WrapKeyHeader};
@@ -37,6 +37,9 @@ pub struct CmdEncrypt {
/// Compatible with 1.0
#[arg(long, short = '1')]
pub compatible_with_1_0: bool,
/// Remove source file
#[arg(long, short = 'R')]
pub remove_source_file: bool,
}
pub fn encrypt(cmd_encrypt: CmdEncrypt) -> XResult<()> {
@@ -120,6 +123,14 @@ fn encrypt_single(path: &PathBuf, envelops: &[&TinyEncryptConfigEnvelop], cmd_en
zeroize(key);
zeroize(nonce);
drop(file_in);
drop(file_out);
if cmd_encrypt.remove_source_file {
match fs::remove_file(path) {
Err(e) => warning!("Remove file: {} failed: {}", path_display, e),
Ok(_) => information!("Remove file: {} succeed", path_display),
}
}
Ok(())
}