feat: update

This commit is contained in:
2023-09-29 20:36:19 +08:00
parent 76af3ed90f
commit 15ffcb9c60
4 changed files with 44 additions and 11 deletions

View File

@@ -1,4 +1,3 @@
use std::fs;
use std::fs::File;
use std::io::{Read, Write};
use std::path::PathBuf;
@@ -46,22 +45,19 @@ pub fn decrypt(cmd_decrypt: CmdDecrypt) -> XResult<()> {
pub fn decrypt_single(path: &PathBuf, pin: &Option<String>, slot: &Option<String>) -> XResult<()> {
let path_display = format!("{}", path.display());
if !path_display.ends_with(TINY_ENC_FILE_EXT) {
return simple_error!("File is not tiny encrypt file: {}", &path_display);
}
util::require_tiny_enc_file_and_exists(path)?;
let mut file_in = opt_result!(File::open(path), "Open file: {} failed: {}", &path_display);
let meta = opt_result!(file::read_tiny_encrypt_meta_and_normalize(&mut file_in), "Read file: {}, failed: {}", &path_display);
let path_out = &path_display[0..path_display.len() - TINY_ENC_FILE_EXT.len()];
if let Ok(_) = fs::metadata(path_out) {
return simple_error!("Output file: {} exists", path_out);
}
util::require_file_not_exists(path_out)?;
debugging!("Found meta: {}", serde_json::to_string_pretty(&meta).unwrap());
let selected_envelop = select_envelop(&meta)?;
let key = try_decrypt_key(selected_envelop, pin, slot)?;
let nonce = opt_result!( decode_base64(&meta.nonce), "Decode nonce failed: {}");
let nonce = opt_result!(decode_base64(&meta.nonce), "Decode nonce failed: {}");
debugging!("Decrypt key: {}", hex::encode(&key));
debugging!("Decrypt nonce: {}", hex::encode(&nonce));
@@ -69,6 +65,8 @@ pub fn decrypt_single(path: &PathBuf, pin: &Option<String>, slot: &Option<String
let mut file_out = File::create(path_out)?;
let _ = decrypt_file(&mut file_in, &mut file_out, &key, &nonce, meta.compress)?;
util::zeroize(key);
util::zeroize(nonce);
Ok(())
}