feat(core): done full ciphering
parent
10a19601a1
commit
69426f220b
@ -0,0 +1,19 @@
|
||||
# Generated by Django 3.2.8 on 2022-05-25 16:46
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('guilds', '0002_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='message',
|
||||
name='nonce',
|
||||
field=models.TextField(default=''),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
@ -1,9 +1,19 @@
|
||||
/* Project specific Javascript goes here. */
|
||||
|
||||
function cipher(text) {
|
||||
return CryptoJS.AES.encrypt(text, me).toString();
|
||||
function constructKeyPair(keyPair) {
|
||||
return nacl.box.keyPair.fromSecretKey(nacl.util.decodeBase64(keyPair[1]));
|
||||
}
|
||||
|
||||
function decipher(text, key) {
|
||||
return CryptoJS.AES.decrypt(text, key).toString(CryptoJS.enc.Utf8);
|
||||
function decipher(message, author) {
|
||||
const sharedKey = nacl.box.before(
|
||||
nacl.util.decodeBase64(recipients[author]), usableKeyPair.secretKey
|
||||
);
|
||||
|
||||
const payload = nacl.box.open.after(
|
||||
nacl.util.decodeBase64(message.box),
|
||||
nacl.util.decodeBase64(message.nonce),
|
||||
sharedKey
|
||||
);
|
||||
|
||||
return nacl.util.encodeUTF8(payload);
|
||||
}
|
||||
|
@ -1 +1 @@
|
||||
function cipher(t){return CryptoJS.AES.encrypt(t,me).toString()}function decipher(t,r){return CryptoJS.AES.decrypt(t,r).toString(CryptoJS.enc.Utf8)}
|
||||
function constructKeyPair(e){return nacl.box.keyPair.fromSecretKey(nacl.util.decodeBase64(e[1]))}function decipher(e,c){const n=nacl.box.before(nacl.util.decodeBase64(recipients[c]),usableKeyPair.secretKey),a=nacl.box.open.after(nacl.util.decodeBase64(e.box),nacl.util.decodeBase64(e.nonce),n);return nacl.util.encodeUTF8(a)}
|
@ -0,0 +1,61 @@
|
||||
document.addEventListener("DOMContentLoaded", () => {
|
||||
let fail_count = 0;
|
||||
|
||||
function process_message(data) {
|
||||
let cmd = data.cmd;
|
||||
let payload = data.data;
|
||||
|
||||
if (!(cmd && payload)) {
|
||||
console.error("Improper ws message received");
|
||||
return;
|
||||
}
|
||||
|
||||
switch (cmd) {
|
||||
case "GIVE_MEMBERS_PUB_KEYS":
|
||||
window.recipients = payload;
|
||||
break;
|
||||
case "MEMBER_JOIN":
|
||||
window.recipients = {...payload, ...recipients};
|
||||
break;
|
||||
case "MEMBER_LEAVE":
|
||||
delete window.recipients[payload];
|
||||
break;
|
||||
case "TEXT_MESSAGE":
|
||||
add_message(payload);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
(function get_websocket() {
|
||||
let chatSocket = new WebSocket(
|
||||
'ws://'
|
||||
+ '127.0.0.1:8000'
|
||||
+ '/ws'
|
||||
+ `/${room_name.guild.id}`
|
||||
+ `/${room_name.id}`
|
||||
);
|
||||
|
||||
chatSocket.onmessage = (e) => {
|
||||
const data = JSON.parse(e.data);
|
||||
process_message(data)
|
||||
};
|
||||
|
||||
chatSocket.onopen = () => {
|
||||
console.log('Websocket connected');
|
||||
fail_count = 0
|
||||
};
|
||||
|
||||
chatSocket.onclose = () => {
|
||||
if (fail_count > 2) {
|
||||
UIkit.modal(document.getElementById("ws_error_modal")).show();
|
||||
} else {
|
||||
fail_count += 1;
|
||||
console.error('Chat socket closed unexpectedly');
|
||||
console.info('Trying to reconnect in 5s...');
|
||||
setTimeout(() => get_websocket(), 5000)
|
||||
}
|
||||
};
|
||||
|
||||
window.ws = chatSocket;
|
||||
})();
|
||||
});
|
Loading…
Reference in New Issue