aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorMarvin Borner2019-01-24 15:41:51 +0100
committerMarvin Borner2019-01-24 15:41:51 +0100
commit9c3e20138d730c167e1869843ec060c7310d8c63 (patch)
tree11b58dc5b5dd700120187f5c3e8c9493d0f9fcd4
parente7af7c49c9cfab9f0da28d953e9b270a40284d43 (diff)
Added basic key transfer
-rw-r--r--public/scripts/2_encryption.js39
-rw-r--r--public/scripts/chat.js49
-rw-r--r--public/styles/style.sass2
-rw-r--r--views/index.pug6
4 files changed, 70 insertions, 26 deletions
diff --git a/public/scripts/2_encryption.js b/public/scripts/2_encryption.js
index 9140a3a..98bd49d 100644
--- a/public/scripts/2_encryption.js
+++ b/public/scripts/2_encryption.js
@@ -5,13 +5,13 @@ let encrypted, decrypted; // REMEMBER: Remove testing variables (leaking)
/**
* Generates and stores encrypted private key, public key and a revocation certificate
- * @param userId
+ * @param peerId
* @param passphrase
* @returns {Promise<void>}
*/
-async function generateKeys(userId, passphrase) {
+async function generateKeys(peerId, passphrase) {
const options = {
- userIds: [{name: userId}],
+ peerIds: [{name: peerId}],
numBits: 4096,
passphrase: passphrase
};
@@ -25,7 +25,23 @@ async function generateKeys(userId, passphrase) {
}
/**
- * Encrypts the data with a public key (e.g the one of the person with which you're chatting)
+ * Gets the peers private key
+ * @returns {string}
+ */
+function getPrivateKey() {
+ return localStorage.getItem('private_key');
+}
+
+/**
+ * Gets the peers public key
+ * @returns {string}
+ */
+function getPublicKey() {
+ return localStorage.getItem('public_key');
+}
+
+/**
+ * Encrypts the data with a public key (e.g the one of the peer with which you're chatting)
* @param data
* @param publicKey
* @returns {Promise<void>}
@@ -73,7 +89,7 @@ async function decrypt(data, publicKey, privateKey, passphrase) {
}
/**
- * Checks whether the user has keys
+ * Checks whether the peer has keys
* @returns {boolean}
*/
function isEncrypted() {
@@ -84,6 +100,16 @@ function isEncrypted() {
}
/**
+ * Stores the public key of a peer
+ * @param peerId
+ * @param key
+ */
+function storePublicKey(peerId, key) {
+ localStorage.setItem(peerId, key);
+ console.log('[LOG] Stored public key of ' + peerId);
+}
+
+/**
* Just a general test case
*/
function testEncryption() {
@@ -98,7 +124,10 @@ function testEncryption() {
}
exports.generate = generateKeys;
+exports.getPrivate = getPrivateKey;
+exports.getPublic = getPublicKey;
exports.encrypt = encrypt;
exports.decrypt = decrypt;
exports.check = isEncrypted;
+exports.store = storePublicKey;
exports.test = testEncryption;
diff --git a/public/scripts/chat.js b/public/scripts/chat.js
index e8b53c0..a31173e 100644
--- a/public/scripts/chat.js
+++ b/public/scripts/chat.js
@@ -2,8 +2,9 @@ const $ = require('jquery');
const encryption = require('./2_encryption');
const nanoid = require('nanoid');
-let connectedUserId, connectedUser;
-const userId = nanoid();
+let connectedPeers = [];
+let connectedPeer;
+const peerId = nanoid();
// setup encryption
if (encryption.check()) {
@@ -11,53 +12,61 @@ if (encryption.check()) {
chat();
} else {
console.log('[LOG] No existing keys found! Generating...');
- encryption.generate(userId, 'supersecure').then(() => chat());
+ encryption.generate(peerId, 'supersecure').then(() => chat());
}
function chat() {
- const peer = new Peer(userId, {host: '127.0.0.1', port: 4242, path: '/', debug: 3});
+ const peer = new Peer(peerId, {host: '127.0.0.1', port: 4242, path: '/', debug: 0});
// 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;
+ connectedPeer = conn;
console.log('[LOG] Connected with', conn.peer);
conn.on('data', message => receivedMessage(message));
});
/**
- * Connects to an user via his id
+ * Connects to a peer 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;
+ connectedPeer = peer.connect(id, {label: connectionId, reliable: true});
// setup listener
- connectedUser.on('open', () => {
+ connectedPeer.on('open', () => {
// TODO: Activate chat or sth
- // TODO: Send public key
+ transferKey(encryption.getPublic());
});
- connectedUser.on('data', message => receivedMessage(message))
+ connectedPeer.on('data', message => receivedMessage(message))
}
/**
- * Sends a message to the user with which you're currently connected
+ * Sends a message to the peer with which you're currently connected
* @param message
*/
function sendMessage(message) {
- console.log(`[LOG] Sending message ${message} to ${connectedUserId}`);
- connectedUser.send(message);
+ console.log(`[LOG] Sending message ${message} to ${connectedPeer.peer}`);
+ connectedPeer.send({type: 'text', data: message});
receivedMessage(message, true);
}
/**
- * Renders the incoming messages
+ * Transfers the (public) key to the currently connected peer
+ * @param key
+ */
+ function transferKey(key) {
+ console.log(`[LOG] Transferring key to ${connectedPeer.peer}`);
+ connectedPeer.send({type: 'key', data: key});
+ }
+
+ /**
+ * Renders and processes the incoming messages
* @param message
* @param self
*/
@@ -65,7 +74,13 @@ function chat() {
if (self) {
$('#messages').append(`<span style="color: green">${message}</span><br>`);
} else {
- $('#messages').append(`${message}<br>`);
+ if (message.type === 'text')
+ $('#messages').append(`${message.data}<br>`);
+ else if (message.type === 'key') {
+ console.log(connectedPeer.peer);
+ console.log(peer.connections);
+ encryption.store(connectedPeer.peer, message.data)
+ }
}
}
@@ -73,7 +88,7 @@ function chat() {
* Events after load
*/
$(document).ready(() => {
- $('#add_user_id').on('click', () => connect($('#user_id').val()));
+ $('#add_peer_id').on('click', () => connect($('#peer_id').val()));
$('#send_message').on('click', () => sendMessage($('#message').val()));
$('[toggle-contact-modal]').on('click', () => $('#add_contact_modal').toggleClass('is-active'))
diff --git a/public/styles/style.sass b/public/styles/style.sass
index 15415fd..16ce7fe 100644
--- a/public/styles/style.sass
+++ b/public/styles/style.sass
@@ -7,7 +7,7 @@ html, body
.main
align-items: flex-start
-.add-user-button
+.add-peer-button
height: 50px
width: 50px
margin: 20px
diff --git a/views/index.pug b/views/index.pug
index 0f3f0ea..edcec7f 100644
--- a/views/index.pug
+++ b/views/index.pug
@@ -1,7 +1,7 @@
extends layout
block content
- button.button.add-user-button.is-big.is-outlined.is-info(toggle-contact-modal)
+ button.button.add-peer-button.is-big.is-outlined.is-info(toggle-contact-modal)
i.fas.fa-plus
.columns.level.main
@@ -19,7 +19,7 @@ block content
p.modal-card-title Add a contact
button.delete(aria-label='close', toggle-contact-modal)
section.modal-card-body
- input#user_id.input.is-focused(placeholder='Contact ID')
+ input#peer_id.input.is-focused(placeholder='Contact ID')
footer.modal-card-foot
- button#add_user_id.button.is-success(toggle-contact-modal) Add
+ button#add_peer_id.button.is-success(toggle-contact-modal) Add
button.button(aria-label='close', toggle-contact-modal) Cancel