encrypt and decrypt messages in browser
This commit is contained in:
@@ -31,12 +31,16 @@ function init_ratchets(order, user_pubkey) {
|
||||
|
||||
}
|
||||
|
||||
socket.on('chat message', (msg, room) => {
|
||||
socket.on('chat message', (msg, room, tag_received, iv, nonce) => {
|
||||
console.log(`received: ${msg}`);
|
||||
console.log(sharedsecret);
|
||||
const item = document.createElement('li');
|
||||
item.textContent = msg;
|
||||
const messages = document.getElementById(`messages-${room}`);
|
||||
const associated_data = fromHexString(Array.from((document.getElementById('pubkey')).classList).find(className => className.startsWith('key-')).replace('key-', ''));
|
||||
const pubkey = Array.from(messages.classList).find(className => className.startsWith('key-')).replace('key-', '');
|
||||
const item = document.createElement('li');
|
||||
let {plaintext, tag} = decrypt_message(msg, pubkey, fromHexString(iv), fromHexString(nonce), associated_data);
|
||||
item.textContent = plaintext
|
||||
console.log(tag);
|
||||
console.log(tag_received);
|
||||
messages.appendChild(item);
|
||||
window.scrollTo(0, document.body.scrollHeight);
|
||||
});
|
||||
@@ -69,12 +73,55 @@ export function create_listener(form, input) {
|
||||
form.addEventListener('submit', function(e) {
|
||||
e.preventDefault();
|
||||
if (input.value) {
|
||||
socket.emit('chat message', input.value, form.id);
|
||||
const pubkey = Array.from(form.classList).find(className => className.startsWith('key-')).replace('key-', '');
|
||||
let {cipher, tag, iv, nonce} = encrypt_message(input.value, pubkey)
|
||||
socket.emit('chat message', cipher, form.id, tag, iv, nonce);
|
||||
input.value = '';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function encrypt_message(message, user_pubkey) {
|
||||
let encryption_key = sending_ratchets[user_pubkey].next();
|
||||
let encoded_msg = (new TextEncoder()).encode(message);
|
||||
let iv = generateRandomUint8Array();
|
||||
let nonce = generateRandomUint8Array();
|
||||
let associated_data = fromHexString(user_pubkey);
|
||||
let {cipher, tag} = keccakAEAD.encrypt(encryption_key, encoded_msg, iv, associated_data, nonce);
|
||||
console.log(iv)
|
||||
console.log(nonce)
|
||||
console.log(associated_data)
|
||||
console.log(encryption_key)
|
||||
console.log(encoded_msg)
|
||||
console.log(cipher)
|
||||
return {cipher: toHexString(cipher),
|
||||
tag: toHexString(tag),
|
||||
iv: toHexString(iv),
|
||||
nonce: toHexString(nonce)
|
||||
};
|
||||
}
|
||||
|
||||
function decrypt_message(cipher, user_pubkey, iv, nonce, associated_data) {
|
||||
let decryption_key = receiving_ratchets[user_pubkey].next();
|
||||
let cipher_array = fromHexString(cipher);
|
||||
let {plaintext, tag} = keccakAEAD.decrypt(decryption_key, cipher_array, iv, associated_data, nonce);
|
||||
console.log(iv)
|
||||
console.log(nonce)
|
||||
console.log(associated_data)
|
||||
console.log(decryption_key)
|
||||
console.log(plaintext)
|
||||
console.log(cipher_array)
|
||||
return {plaintext: (new TextDecoder('utf-8')).decode(plaintext),
|
||||
tag: toHexString(tag)
|
||||
};
|
||||
}
|
||||
|
||||
function generateRandomUint8Array(length = 16) {
|
||||
const randomArray = new Uint8Array(length);
|
||||
window.crypto.getRandomValues(randomArray);
|
||||
return randomArray;
|
||||
}
|
||||
|
||||
const fromHexString = (hexString) =>
|
||||
Uint8Array.from(hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16)));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user