summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs28
1 files changed, 21 insertions, 7 deletions
diff --git a/src/lib.rs b/src/lib.rs
index c422e2b..79c0658 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -3,8 +3,6 @@ mod utils;
use wasm_bindgen::prelude::*;
use std::io::{Read, Write};
-use std::iter;
-
use age::x25519::Recipient;
@@ -17,7 +15,7 @@ use age::x25519::Recipient;
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
#[wasm_bindgen]
-extern {
+extern "C" {
fn alert(s: &str);
}
@@ -26,13 +24,11 @@ pub fn greet() {
alert("Hello, {{project-name}}!");
}
-
#[wasm_bindgen]
-pub fn lalala(plaintext : String) -> Option<Vec<u8>> {
-
+pub fn age_encrypt(plaintext: String, key: String) -> Option<Vec<u8>> {
utils::set_panic_hook();
- let pubkey = "age1m6k5wqnrk63wxhlwtl7s244ngmacn6xph3lxjd3x735f6wm8z4lsysfzg5".parse::<Recipient>().ok()?;
+ let pubkey = key.parse::<Recipient>().ok()?;
let encryptor = age::Encryptor::with_recipients(vec![Box::new(pubkey)]);
@@ -45,3 +41,21 @@ pub fn lalala(plaintext : String) -> Option<Vec<u8>> {
Some(encrypted)
}
+
+#[wasm_bindgen]
+pub fn age_decrypt_passphrase(blob: Vec<u8>, passphrase: String) -> Option<String> {
+ utils::set_panic_hook();
+
+
+ let decryptor = match age::Decryptor::new(&blob[..]).unwrap() {
+ age::Decryptor::Passphrase(d) => d,
+ _ => panic!("something very wrong happened!"),
+ };
+
+ let mut decrypted = vec![];
+ let mut reader = decryptor
+ .decrypt(&secrecy::Secret::new(passphrase), None)
+ .ok()?;
+ reader.read_to_end(&mut decrypted).ok()?;
+ Some(std::str::from_utf8(&decrypted).ok()?.to_owned())
+}