-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (51 loc) · 2.88 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import init, { enc, gen_pub_key, gen_priv_key, dec, b64_encode, b64_decode } from "./enc_chat.js";
await init();
let log = document.getElementById("log");
log.innerHTML = "Welcome!";
if (localStorage.getItem("recipient") !== null) {
document.getElementById("recipient").value = localStorage.getItem("recipient");
}
if (localStorage.getItem("priv_key") === null) {
// Generate a new private key
log.innerHTML = "Generating new keypair";
let priv_key = gen_priv_key();
localStorage.setItem("priv_key", priv_key);
}
let priv_key = localStorage.getItem("priv_key");
let pub_key = gen_pub_key(priv_key);
function encrypt_and_send(message) {
if (localStorage.getItem(document.getElementById("recipient").value) != null ) {
log.innerHTML = "Encrypting message...";
let enc_result = enc(message, localStorage.getItem(document.getElementById("recipient").value));
localStorage.setItem("recipient", document.getElementById("recipient").value);
log.innerHTML = "Encrypted and sent message!";
window.open("mailto:" + document.getElementById("recipient").value + "?subject=" + b64_encode(pub_key) + "&body=" + enc_result);
} else {
log.innerHTML = "Recipient public key not found! Please click the \"Add Recipient\" button below and paste the recipient's public key (found in the subject of their email message)";
}
}
function decrypt(message) {
log.innerHTML = "Decrypting message...";
let dec_result = dec(message, localStorage.getItem("priv_key"));
log.innerHTML = "Message received: " + dec_result;
}
function add_recipient() {
let email = document.getElementById("recipient").value;
let pub_key = prompt("Recipient public key (found in subject of email):");
if (email == "" || email == null || pub_key == null || pub_key == "") {
log.innerHTML = "Invalid email or public key";
} else {
localStorage.setItem(email, b64_decode(pub_key));
alert(localStorage.getItem(email));
}
log.innerHTML = "";
}
function clear_data() {
localStorage.clear();
log.innerHTML = "Data cleared successfully! Please refresh the page";
}
document.getElementById("send_message").onclick = function() { encrypt_and_send(document.getElementById("message").value); };
document.getElementById("decrypt_message").onclick = function() { decrypt(document.getElementById("message").value); };
document.getElementById("add_recipient").onclick = function() { add_recipient(); };
document.getElementById("clear_data").onclick = function() { clear_data(); };
document.getElementById("show_pubkey").onclick = function() { log.innerHTML = b64_encode(pub_key); };