feat: works
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
*.tinyenc
|
||||||
@@ -2,7 +2,7 @@
|
|||||||
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
|
||||||
|
|
||||||
use aes_gcm_stream::{Aes256GcmStreamDecryptor, Aes256GcmStreamEncryptor};
|
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;
|
use std::fs;
|
||||||
|
|
||||||
const ALGORITHM_AES_256_GCM: &str = "aes-256-gcm";
|
const ALGORITHM_AES_256_GCM: &str = "aes-256-gcm";
|
||||||
@@ -51,8 +51,9 @@ impl EncryptArgs {
|
|||||||
fn read(&self) -> XResult<Vec<u8>> {
|
fn read(&self) -> XResult<Vec<u8>> {
|
||||||
let file_content = opt_result!(fs::read(&self.file_name), "Read from file: {} failed: {}", &self.file_name);
|
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);
|
let mut decryptor = Aes256GcmStreamDecryptor::new(self.key.clone(), &self.nonce);
|
||||||
decryptor.update(&file_content);
|
let mut plaintext = vec![];
|
||||||
let plaintext = opt_result!(decryptor.finalize(), "Decrypt input file failed: {}");
|
plaintext.extend_from_slice(&decryptor.update(&file_content));
|
||||||
|
plaintext.extend_from_slice(&opt_result!(decryptor.finalize(), "Decrypt input file failed: {}"));
|
||||||
|
|
||||||
Ok(plaintext)
|
Ok(plaintext)
|
||||||
}
|
}
|
||||||
@@ -63,26 +64,46 @@ impl EncryptArgs {
|
|||||||
|
|
||||||
fn write(&self, content: &[u8]) -> XResult<()> {
|
fn write(&self, content: &[u8]) -> XResult<()> {
|
||||||
let mut encryptor = Aes256GcmStreamEncryptor::new(self.key.clone(), &self.nonce);
|
let mut encryptor = Aes256GcmStreamEncryptor::new(self.key.clone(), &self.nonce);
|
||||||
encryptor.update(content);
|
let mut ciphertext = vec![];
|
||||||
let (mut ciphertext, tag) = encryptor.finalize();
|
ciphertext.extend_from_slice(&encryptor.update(content));
|
||||||
|
let (final_block, tag) = encryptor.finalize();
|
||||||
|
ciphertext.extend_from_slice(&final_block);
|
||||||
ciphertext.extend_from_slice(&tag);
|
ciphertext.extend_from_slice(&tag);
|
||||||
opt_result!(fs::write(&self.file_name, &ciphertext), "Write to file: {} failed: {}", &self.file_name);
|
opt_result!(fs::write(&self.file_name, &ciphertext), "Write to file: {} failed: {}", &self.file_name);
|
||||||
Ok(())
|
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]
|
#[tauri::command]
|
||||||
fn read_content() -> String {
|
fn read_content() -> String {
|
||||||
// TODO ...
|
match inner_read_content() {
|
||||||
let args: Vec<String> = std::env::args().collect();
|
Ok(content) => content,
|
||||||
args.join(", ")
|
Err(e) => failure_and_exit!("Read content failed: {}", e)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tauri::command]
|
#[tauri::command]
|
||||||
fn save_content(content: &str) -> String {
|
fn save_content(content: &str) -> String {
|
||||||
// TOD save....
|
match inner_write_content(content) {
|
||||||
format!("Hello, {}! You've been greeted from Rust!", 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() {
|
fn main() {
|
||||||
@@ -90,6 +111,7 @@ fn main() {
|
|||||||
.invoke_handler(tauri::generate_handler![
|
.invoke_handler(tauri::generate_handler![
|
||||||
read_content,
|
read_content,
|
||||||
save_content,
|
save_content,
|
||||||
|
do_exit,
|
||||||
])
|
])
|
||||||
.run(tauri::generate_context!())
|
.run(tauri::generate_context!())
|
||||||
.expect("error while running tauri application");
|
.expect("error while running tauri application");
|
||||||
|
|||||||
@@ -10,52 +10,13 @@
|
|||||||
|
|
||||||
<body>
|
<body>
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<table style="width: 100%; height: 100%;">
|
<h1>Secure Editor</h1>
|
||||||
<tr>
|
<textarea id="content" style="margin: 1vh 1vw; padding: 1vh;"></textarea>
|
||||||
<th>header</th>
|
<div style="text-align: right; margin: 0.5vh 1vw;">
|
||||||
</tr>
|
<button id="exit">Exit</button>
|
||||||
<tr>
|
<button id="save"> Save </button>
|
||||||
<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>
|
|
||||||
</div>
|
</div>
|
||||||
|
<p id="message" style="text-align: left;margin: 0 1vw;">-</p>
|
||||||
<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> -->
|
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
16
src/main.js
16
src/main.js
@@ -1,23 +1,29 @@
|
|||||||
const { invoke } = window.__TAURI__.tauri;
|
const { invoke } = window.__TAURI__.tauri;
|
||||||
|
|
||||||
let contentEl;
|
let contentEl;
|
||||||
let greetMsgEl;
|
let messageEl;
|
||||||
|
|
||||||
async function init_content() {
|
async function init_content() {
|
||||||
contentEl.value = await invoke("read_content");
|
contentEl.value = await invoke("read_content");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function save_content() {
|
async function save_content() {
|
||||||
contentEl.value = "test";
|
await invoke("save_content", { content: contentEl.value });
|
||||||
//greetMsgEl.textContent = await invoke("save_content", { name: greetInputEl.value }) + ' >>>';// + await invoke("read_content");
|
}
|
||||||
|
|
||||||
|
async function do_exit() {
|
||||||
|
contentEl.value = await invoke("do_exit");
|
||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("DOMContentLoaded", () => {
|
window.addEventListener("DOMContentLoaded", () => {
|
||||||
contentEl = document.querySelector("#content");
|
contentEl = document.querySelector("#content");
|
||||||
contentEl.value = "aaaa";
|
messageEl = document.querySelector("#message");
|
||||||
//greetMsgEl = document.querySelector("#greet-msg");
|
|
||||||
init_content();
|
init_content();
|
||||||
document.querySelector("#save").addEventListener("click", (e) => {
|
document.querySelector("#save").addEventListener("click", (e) => {
|
||||||
save_content();
|
save_content();
|
||||||
});
|
});
|
||||||
|
document.querySelector("#exit").addEventListener("click", (e) => {
|
||||||
|
do_exit();
|
||||||
|
});
|
||||||
|
messageEl.innerText = 'init success'
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,24 +17,17 @@
|
|||||||
-webkit-text-size-adjust: 100%;
|
-webkit-text-size-adjust: 100%;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
body {
|
||||||
|
margin: 3vh 1vw;
|
||||||
|
}
|
||||||
|
|
||||||
.container {
|
.container {
|
||||||
margin: 0;
|
|
||||||
padding-top: 10vh;
|
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
width: 98vw;
|
||||||
|
height: 94vh;
|
||||||
.logo {
|
|
||||||
height: 6em;
|
|
||||||
padding: 1.5em;
|
|
||||||
will-change: filter;
|
|
||||||
transition: 0.75s;
|
|
||||||
}
|
|
||||||
|
|
||||||
.logo.tauri:hover {
|
|
||||||
filter: drop-shadow(0 0 2em #24c8db);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.row {
|
.row {
|
||||||
@@ -54,10 +47,23 @@ a:hover {
|
|||||||
|
|
||||||
h1 {
|
h1 {
|
||||||
text-align: center;
|
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 {
|
button {
|
||||||
border-radius: 8px;
|
border-radius: 8px;
|
||||||
border: 1px solid transparent;
|
border: 1px solid transparent;
|
||||||
@@ -78,19 +84,21 @@ button {
|
|||||||
button:hover {
|
button:hover {
|
||||||
border-color: #396cd8;
|
border-color: #396cd8;
|
||||||
}
|
}
|
||||||
|
|
||||||
button:active {
|
button:active {
|
||||||
border-color: #396cd8;
|
border-color: #396cd8;
|
||||||
background-color: #e8e8e8;
|
background-color: #e8e8e8;
|
||||||
}
|
}
|
||||||
|
|
||||||
input,
|
|
||||||
textarea,
|
textarea,
|
||||||
button {
|
button {
|
||||||
outline: none;
|
outline: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
#greet-input {
|
#content {
|
||||||
margin-right: 5px;
|
margin-left: 0.1vw;
|
||||||
|
margin-right: 0.1vw;
|
||||||
|
height: 80vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
@media (prefers-color-scheme: dark) {
|
@media (prefers-color-scheme: dark) {
|
||||||
|
|||||||
Reference in New Issue
Block a user