From 3a876b2f3bd7d41b3a720ac48a831d41c425dbbc Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Wed, 23 Jan 2019 18:05:42 +0100 Subject: Added basic encryption functions --- public/scripts/2_encryption.js | 71 ++++++++++++++++++++++++++++++++++++++++++ public/scripts/chat.js | 68 ++++++++++++++++++++++++++++++++++++++++ public/scripts/main.js | 68 ---------------------------------------- 3 files changed, 139 insertions(+), 68 deletions(-) create mode 100644 public/scripts/2_encryption.js create mode 100644 public/scripts/chat.js delete mode 100644 public/scripts/main.js (limited to 'public') 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} + */ +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/chat.js b/public/scripts/chat.js new file mode 100644 index 0000000..b2f2816 --- /dev/null +++ b/public/scripts/chat.js @@ -0,0 +1,68 @@ +const $ = require('jquery'); +const nanoid = require('nanoid'); + +let connectedUserId, connectedUser; +const userId = nanoid(); +const peer = new Peer(userId, {host: '127.0.0.1', port: 4242, path: '/', debug: 3}); + +// Peer events +peer.on('open', id => console.log('[LOG] Your ID is', id)); +peer.on('error', err => console.error(err)); +peer.on('connection', conn => { + connectedUser = conn; + console.log('[LOG] Connected with', conn.peer); + conn.on('data', message => receivedMessage(message)); +}); + +/** + * Connects to an user via his id + * @param id + */ +function connect(id) { + const connectionId = nanoid(); + console.log('[LOG] Connecting to', id); + console.log('[LOG] Your connection ID is', connectionId); + connectedUser = peer.connect(id, {label: connectionId, reliable: true}); + connectedUserId = id; + + // setup listener + connectedUser.on('open', () => { + // TODO: Activate chat or sth + // TODO: Send public key + }); + + connectedUser.on('data', message => receivedMessage(message)) +} + +/** + * Sends a message to the user with which you're currently connected + * @param message + */ +function sendMessage(message) { + console.log(`[LOG] Sending message ${message} to ${connectedUserId}`); + connectedUser.send(message); + receivedMessage(message, true); +} + +/** + * Renders the incoming messages + * @param message + * @param self + */ +function receivedMessage(message, self = false) { + if (self) { + $('#messages').append(`${message}
`); + } else { + $('#messages').append(`${message}
`); + } +} + +/** + * Events after load + */ +$(document).ready(() => { + $('#add_user_id').on('click', () => connect($('#user_id').val())); + $('#send_message').on('click', () => sendMessage($('#message').val())); + + $('[toggle-contact-modal]').on('click', () => $('#add_contact_modal').toggleClass('is-active')) +}); diff --git a/public/scripts/main.js b/public/scripts/main.js deleted file mode 100644 index b2f2816..0000000 --- a/public/scripts/main.js +++ /dev/null @@ -1,68 +0,0 @@ -const $ = require('jquery'); -const nanoid = require('nanoid'); - -let connectedUserId, connectedUser; -const userId = nanoid(); -const peer = new Peer(userId, {host: '127.0.0.1', port: 4242, path: '/', debug: 3}); - -// Peer events -peer.on('open', id => console.log('[LOG] Your ID is', id)); -peer.on('error', err => console.error(err)); -peer.on('connection', conn => { - connectedUser = conn; - console.log('[LOG] Connected with', conn.peer); - conn.on('data', message => receivedMessage(message)); -}); - -/** - * Connects to an user via his id - * @param id - */ -function connect(id) { - const connectionId = nanoid(); - console.log('[LOG] Connecting to', id); - console.log('[LOG] Your connection ID is', connectionId); - connectedUser = peer.connect(id, {label: connectionId, reliable: true}); - connectedUserId = id; - - // setup listener - connectedUser.on('open', () => { - // TODO: Activate chat or sth - // TODO: Send public key - }); - - connectedUser.on('data', message => receivedMessage(message)) -} - -/** - * Sends a message to the user with which you're currently connected - * @param message - */ -function sendMessage(message) { - console.log(`[LOG] Sending message ${message} to ${connectedUserId}`); - connectedUser.send(message); - receivedMessage(message, true); -} - -/** - * Renders the incoming messages - * @param message - * @param self - */ -function receivedMessage(message, self = false) { - if (self) { - $('#messages').append(`${message}
`); - } else { - $('#messages').append(`${message}
`); - } -} - -/** - * Events after load - */ -$(document).ready(() => { - $('#add_user_id').on('click', () => connect($('#user_id').val())); - $('#send_message').on('click', () => sendMessage($('#message').val())); - - $('[toggle-contact-modal]').on('click', () => $('#add_contact_modal').toggleClass('is-active')) -}); -- cgit v1.2.3