-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
81 lines (66 loc) · 2.28 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
74
75
76
77
78
79
80
81
var admin = require("firebase-admin");
var openpgp = require("openpgp"); // use as CommonJS, AMD, ES6 module or via window.openpgp
openpgp.initWorker({ path: "openpgp.worker.js" }); // set the relative web worker path
// Set Firebase Admin options
// let serviceAccount = require("./fraser-votes-5da49fb5e231.json");
// admin.initializeApp({
// credential: admin.credential.cert(serviceAccount),
// });
// Generate Keys
let genKeys = (name, email) => {
// Set Key Options
var genOptions = {
userIds: [{ name: name, email: email }], // multiple user IDs
};
let keys = openpgp.generateKey(genOptions).then(function (key) {
var privkey = key.privateKeyArmored; // '-----BEGIN PGP PRIVATE KEY BLOCK ... '
var pubkey = key.publicKeyArmored; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
var revocationCertificate = key.revocationCertificate; // '-----BEGIN PGP PUBLIC KEY BLOCK ... '
return { privkey, pubkey, revocationCertificate };
});
return keys;
};
// Encrypt Data
let encrypt = async (plaintext, pubkey) => {
// Encrypt a test string with public key
const encOptions = {
message: await openpgp.message.fromText(plaintext),
publicKeys: (await openpgp.key.readArmored(pubkey)).keys,
};
let encrypted = await openpgp.encrypt(encOptions).then((ciphertext) => {
return ciphertext.data;
});
return encrypted;
};
// Upload keys to server, print private key to console
// TODO: Implement
// Ask user for private key again
// TODO: Implement
// Decrypt Data
let decrypt = async (ciphertext, privkey) => {
// Decrypt test string, check if it matches
const decOptions = {
message: await openpgp.message.readArmored(ciphertext),
privateKeys: (await openpgp.key.readArmored(privkey)).keys,
};
let decrypted = await openpgp.decrypt(decOptions).then((plaintext) => {
return plaintext.data;
});
return decrypted;
};
const testString = "Fraser Votes 2020";
genKeys("Jon Smith", "[email protected]")
.then((keys) => {
console.log(keys);
return encrypt(testString, keys.pubkey).then((ciphertext) => {
console.log(ciphertext);
return decrypt(ciphertext, keys.privkey);
});
})
.then((plaintext) => {
console.log(plaintext);
if (plaintext === testString) {
console.log("Check Succeeded!");
}
});
console.log("DONE");