diff options
author | Marvin Borner | 2019-01-23 18:05:42 +0100 |
---|---|---|
committer | Marvin Borner | 2019-01-23 18:05:42 +0100 |
commit | 3a876b2f3bd7d41b3a720ac48a831d41c425dbbc (patch) | |
tree | 6855e49d6306190ab57b90c014638b26de84f448 /public | |
parent | 6f9cd73f5b8273ab659554e541e52af786789bac (diff) |
Added basic encryption functions
Diffstat (limited to 'public')
-rw-r--r-- | public/scripts/2_encryption.js | 71 | ||||
-rw-r--r-- | public/scripts/chat.js (renamed from public/scripts/main.js) | 0 |
2 files changed, 71 insertions, 0 deletions
diff --git a/public/scripts/2_encryption.js b/public/scripts/2_encryption.js new file mode 100644 index 0000000..b678606 --- /dev/null +++ b/public/scripts/2_encryption.js @@ -0,0 +1,71 @@ +const openpgp = require('openpgp'); +//openpgp.initWorker({ path:'openpgp.worker.js' }); // TODO: Add openpgp web worker support + +let encrypted, decrypted; // REMEMBER: Remove testing variables (leaking) + +/** + * Generates and stores encrypted private key, public key and a revocation certificate + * @param userId + * @param passphrase + * @returns {Promise<void>} + */ +async function generateKeys(userId, passphrase) { + const options = { + userIds: [{name: userId}], + numBits: 4096, + passphrase: passphrase + }; + + openpgp.generateKey(options).then((key) => { + localStorage.setItem('private_key', key.privateKeyArmored); + localStorage.setItem('public_key', key.publicKeyArmored); + localStorage.setItem('revocation_certificate', key.revocationCertificate); + }); +} + +async function encrypt(data, publicKey) { + //const privateKeyObj = (await openpgp.key.readArmored(privateKey)).keys[0]; + //await privateKeyObj.decrypt(passphrase); + + const options = { + message: openpgp.message.fromText(data), + publicKeys: (await openpgp.key.readArmored(publicKey)).keys, + //privateKeys: [privateKeyObj] TODO: Use private key for signing + }; + + await openpgp.encrypt(options).then(ciphertext => { + encrypted = ciphertext.data; + console.log(encrypted); + //return encrypted; // TODO: Return encrypted from async function + }) +} + +async function decrypt(data, publicKey, privateKey, passphrase) { + const privateKeyObj = (await openpgp.key.readArmored(privateKey)).keys[0]; + await privateKeyObj.decrypt(passphrase); + + const options = { + message: await openpgp.message.readArmored(data), + publicKeys: (await openpgp.key.readArmored(publicKey)).keys, // for verification + privateKeys: [privateKeyObj] + }; + + await openpgp.decrypt(options).then(plaintext => { + decrypted = plaintext.data; + console.log(plaintext.data); + //return plaintext.data + }) +} + +function testEncryption() { + generateKeys('test_id', 'supersecure').then(() => { + encrypt('The meaning of life', localStorage.getItem('public_key')).then(() => { + decrypt(encrypted, localStorage.getItem('public_key'), localStorage.getItem('private_key'), 'supersecure').then(() => { + if (decrypted === 'The meaning of life') + console.log("YEEHA, Test succeeded!") + }) + }) + }) +} + +testEncryption(); diff --git a/public/scripts/main.js b/public/scripts/chat.js index b2f2816..b2f2816 100644 --- a/public/scripts/main.js +++ b/public/scripts/chat.js |