feat: updates
This commit is contained in:
@@ -1,6 +1,76 @@
|
||||
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
|
||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||
|
||||
use aes_gcm_stream::{Aes256GcmStreamDecryptor, Aes256GcmStreamEncryptor};
|
||||
use rust_util::{opt_result, simple_error, XResult};
|
||||
use std::fs;
|
||||
|
||||
const ALGORITHM_AES_256_GCM: &str = "aes-256-gcm";
|
||||
|
||||
struct EncryptArgs {
|
||||
file_name: String,
|
||||
key: [u8; 32],
|
||||
nonce: Vec<u8>,
|
||||
}
|
||||
|
||||
impl EncryptArgs {
|
||||
fn new() -> XResult<EncryptArgs> {
|
||||
let args: Vec<String> = std::env::args().collect();
|
||||
if args.len() != 5 {
|
||||
return simple_error!("Bad encrypt args: {:?}", args);
|
||||
}
|
||||
let file_name = args[1].clone();
|
||||
let algorithm = args[2].clone();
|
||||
if algorithm.as_str() != ALGORITHM_AES_256_GCM {
|
||||
return simple_error!("Bad algorithm: {}", algorithm);
|
||||
}
|
||||
let key_bytes = opt_result!(hex::decode(&args[3]), "Bad key: {} failed: {}", &args[3]);
|
||||
if key_bytes.len() != 32 {
|
||||
return simple_error!("Bad key length: {}", key_bytes.len());
|
||||
}
|
||||
let nonce = opt_result!(hex::decode(&args[4]), "Bad nonce: {} failed: {}", &args[4]);
|
||||
if nonce.len() != 12 {
|
||||
return simple_error!("Bad nonce length: {}", nonce.len());
|
||||
}
|
||||
let mut key = [0_u8; 32];
|
||||
for i in 0..32 {
|
||||
key[i] = key_bytes[i];
|
||||
}
|
||||
|
||||
Ok(EncryptArgs {
|
||||
file_name,
|
||||
key,
|
||||
nonce,
|
||||
})
|
||||
}
|
||||
|
||||
fn read_to_string(&self) -> XResult<String> {
|
||||
Ok(opt_result!(String::from_utf8(self.read()?), "Read from file: {} failed: {} (not UTF-8)", &self.file_name))
|
||||
}
|
||||
|
||||
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: {}");
|
||||
|
||||
Ok(plaintext)
|
||||
}
|
||||
|
||||
fn write_string(&self, content: &str) -> XResult<()> {
|
||||
self.write(content.as_bytes())
|
||||
}
|
||||
|
||||
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();
|
||||
ciphertext.extend_from_slice(&tag);
|
||||
opt_result!(fs::write(&self.file_name, &ciphertext), "Write to file: {} failed: {}", &self.file_name);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[tauri::command]
|
||||
fn read_content() -> String {
|
||||
@@ -10,9 +80,9 @@ fn read_content() -> String {
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn save_content(name: &str) -> String {
|
||||
fn save_content(content: &str) -> String {
|
||||
// TOD save....
|
||||
format!("Hello, {}! You've been greeted from Rust!", name)
|
||||
format!("Hello, {}! You've been greeted from Rust!", content)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
|
||||
Reference in New Issue
Block a user