From 23b5db16ce09eb8a73b439a604f49fc7018df53c Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sun, 4 Apr 2021 02:48:06 +0200 Subject: usable interface for encryption (pubkey) and decryption (passphrase) --- src/lib.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) (limited to 'src') 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> { - +pub fn age_encrypt(plaintext: String, key: String) -> Option> { utils::set_panic_hook(); - let pubkey = "age1m6k5wqnrk63wxhlwtl7s244ngmacn6xph3lxjd3x735f6wm8z4lsysfzg5".parse::().ok()?; + let pubkey = key.parse::().ok()?; let encryptor = age::Encryptor::with_recipients(vec![Box::new(pubkey)]); @@ -45,3 +41,21 @@ pub fn lalala(plaintext : String) -> Option> { Some(encrypted) } + +#[wasm_bindgen] +pub fn age_decrypt_passphrase(blob: Vec, passphrase: String) -> Option { + 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()) +} -- cgit v1.2.3