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

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.tinyenc

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");

View File

@@ -10,52 +10,13 @@
<body>
<div class="container">
<table style="width: 100%; height: 100%;">
<tr>
<th>header</th>
</tr>
<tr>
<td>
<textarea id="content" style="margin: 1vh; padding: 1vh;"></textarea>
</td>
</tr>
<tr>
<td style="text-align: right;">
<button id="save">Save</button>
</td>
</tr>
<tr>
<td>
foot
</td>
</tr>
</table>
<!-- <h1>Welcome to Tauri! XXX</h1>
<div class="row">
<a href="https://tauri.app" target="_blank">
<img src="/assets/tauri.svg" class="logo tauri" alt="Tauri logo" />
</a>
<a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript"
target="_blank"
>
<img
src="/assets/javascript.svg"
class="logo vanilla"
alt="JavaScript logo"
/>
</a>
<h1>Secure Editor</h1>
<textarea id="content" style="margin: 1vh 1vw; padding: 1vh;"></textarea>
<div style="text-align: right; margin: 0.5vh 1vw;">
<button id="exit">Exit</button>
<button id="save">&nbsp; Save &nbsp;</button>
</div>
<p>Click on the Tauri logo to learn more about the framework</p>
<form class="row" id="greet-form">
<input id="greet-input" placeholder="Enter a name..." />
<button type="submit">Greet</button>
</form>
<p id="greet-msg"></p> -->
<p id="message" style="text-align: left;margin: 0 1vw;">-</p>
</div>
</body>
</html>

View File

@@ -1,23 +1,29 @@
const { invoke } = window.__TAURI__.tauri;
let contentEl;
let greetMsgEl;
let messageEl;
async function init_content() {
contentEl.value = await invoke("read_content");
}
async function save_content() {
contentEl.value = "test";
//greetMsgEl.textContent = await invoke("save_content", { name: greetInputEl.value }) + ' >>>';// + await invoke("read_content");
await invoke("save_content", { content: contentEl.value });
}
async function do_exit() {
contentEl.value = await invoke("do_exit");
}
window.addEventListener("DOMContentLoaded", () => {
contentEl = document.querySelector("#content");
contentEl.value = "aaaa";
//greetMsgEl = document.querySelector("#greet-msg");
messageEl = document.querySelector("#message");
init_content();
document.querySelector("#save").addEventListener("click", (e) => {
save_content();
});
document.querySelector("#exit").addEventListener("click", (e) => {
do_exit();
});
messageEl.innerText = 'init success'
});

View File

@@ -17,24 +17,17 @@
-webkit-text-size-adjust: 100%;
}
body {
margin: 3vh 1vw;
}
.container {
margin: 0;
padding-top: 10vh;
display: flex;
flex-direction: column;
justify-content: center;
text-align: center;
}
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: 0.75s;
}
.logo.tauri:hover {
filter: drop-shadow(0 0 2em #24c8db);
width: 98vw;
height: 94vh;
}
.row {
@@ -54,10 +47,23 @@ a:hover {
h1 {
text-align: center;
margin-top: 0.1vh;
margin-bottom: 0.1vh;
}
textarea {
border-radius: 8px;
border: 1px solid transparent;
padding: 0;
font-size: 1em;
font-weight: 500;
font-family: inherit;
color: #0f0f0f;
background-color: #ffffff;
transition: border-color 0.25s;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2);
}
input,
textarea,
button {
border-radius: 8px;
border: 1px solid transparent;
@@ -78,19 +84,21 @@ button {
button:hover {
border-color: #396cd8;
}
button:active {
border-color: #396cd8;
background-color: #e8e8e8;
}
input,
textarea,
button {
outline: none;
}
#greet-input {
margin-right: 5px;
#content {
margin-left: 0.1vw;
margin-right: 0.1vw;
height: 80vh;
}
@media (prefers-color-scheme: dark) {