feat: works

This commit is contained in:
2024-08-21 00:46:21 +08:00
parent 67ce25313e
commit 51c38cea41
5 changed files with 76 additions and 78 deletions

View File

@@ -2,7 +2,7 @@
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use aes_gcm_stream::{Aes256GcmStreamDecryptor, Aes256GcmStreamEncryptor};
use rust_util::{opt_result, simple_error, XResult};
use rust_util::{failure_and_exit, opt_result, simple_error, XResult};
use std::fs;
const ALGORITHM_AES_256_GCM: &str = "aes-256-gcm";
@@ -51,8 +51,9 @@ impl EncryptArgs {
fn read(&self) -> XResult<Vec<u8>> {
let file_content = opt_result!(fs::read(&self.file_name), "Read from file: {} failed: {}", &self.file_name);
let mut decryptor = Aes256GcmStreamDecryptor::new(self.key.clone(), &self.nonce);
decryptor.update(&file_content);
let plaintext = opt_result!(decryptor.finalize(), "Decrypt input file failed: {}");
let mut plaintext = vec![];
plaintext.extend_from_slice(&decryptor.update(&file_content));
plaintext.extend_from_slice(&opt_result!(decryptor.finalize(), "Decrypt input file failed: {}"));
Ok(plaintext)
}
@@ -63,26 +64,46 @@ impl EncryptArgs {
fn write(&self, content: &[u8]) -> XResult<()> {
let mut encryptor = Aes256GcmStreamEncryptor::new(self.key.clone(), &self.nonce);
encryptor.update(content);
let (mut ciphertext, tag) = encryptor.finalize();
let mut ciphertext = vec![];
ciphertext.extend_from_slice(&encryptor.update(content));
let (final_block, tag) = encryptor.finalize();
ciphertext.extend_from_slice(&final_block);
ciphertext.extend_from_slice(&tag);
opt_result!(fs::write(&self.file_name, &ciphertext), "Write to file: {} failed: {}", &self.file_name);
Ok(())
}
}
fn inner_read_content() -> XResult<String> {
let encrypt_args = EncryptArgs::new()?;
encrypt_args.read_to_string()
}
fn inner_write_content(content: &str) -> XResult<()> {
let encrypt_args = EncryptArgs::new()?;
encrypt_args.write_string(content)
}
#[tauri::command]
fn read_content() -> String {
// TODO ...
let args: Vec<String> = std::env::args().collect();
args.join(", ")
match inner_read_content() {
Ok(content) => content,
Err(e) => failure_and_exit!("Read content failed: {}", e)
}
}
#[tauri::command]
fn save_content(content: &str) -> String {
// TOD save....
format!("Hello, {}! You've been greeted from Rust!", content)
match inner_write_content(content) {
Ok(_) => std::process::exit(0),
Err(e) => failure_and_exit!("Write content failed: {}", e),
}
}
#[tauri::command]
fn do_exit() -> String {
std::process::exit(-1);
}
fn main() {
@@ -90,6 +111,7 @@ fn main() {
.invoke_handler(tauri::generate_handler![
read_content,
save_content,
do_exit,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");