Created
October 20, 2018 13:41
-
-
Save goncalo-ribeiro/b84fca008b3948cd3061a5c47b1c9b65 to your computer and use it in GitHub Desktop.
webchat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<title>Socket.IO chat</title> | |
<style> | |
* { margin: 0; padding: 0; box-sizing: border-box; } | |
body { font: 13px Helvetica, Arial; } | |
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; } | |
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; } | |
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; } | |
#messages { list-style-type: none; margin: 0; padding: 0; } | |
#messages li { padding: 5px 10px; } | |
#messages li:nth-child(odd) { background: #eee; } | |
#messages { margin-bottom: 40px } | |
</style> | |
</head> | |
<body> | |
<ul id="messages"></ul> | |
<form action=""> | |
<input id="m" autocomplete="off" /><button>Send</button> | |
</form> | |
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script> | |
<script src="https://code.jquery.com/jquery-1.11.1.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/node-forge@0.7.0/dist/forge.min.js"></script> | |
<script> | |
var key, iv; | |
$(function () { | |
var socket = io(); | |
$('form').submit(function(){ | |
var input = $('#m').val(); | |
console.log('input = ' + input); | |
var encryptedInput = encrypt(input); | |
console.log(encryptedInput) | |
socket.emit('chat message', encryptedInput); | |
$('#m').val(''); | |
return false; | |
}); | |
socket.on('symetric_key', function(data){ | |
console.log(data) | |
key = data.key; | |
iv = data.iv; | |
}); | |
socket.on('chat message', function(msg){ | |
console.log('received a new message!') | |
console.log(msg) | |
decrypt(msg) | |
}); | |
function encrypt (bytesToEncrypt){ | |
cipher = forge.cipher.createCipher('AES-CBC', key); | |
cipher.start({'iv': iv}); | |
cipher.update(forge.util.createBuffer(bytesToEncrypt)); | |
cipher.finish(); | |
var encrypted = cipher.output; | |
console.log(bytesToEncrypt + ' => ' + encrypted.toHex()); | |
return encrypted | |
} | |
function decrypt (bytesToDecrypt){ | |
decipher = forge.cipher.createDecipher('AES-CBC', key); | |
decipher.start({'iv': iv}); | |
decipher.update(bytesToDecrypt); | |
var result = decipher.finish(); // check 'result' for true/false | |
console.log('decryption result = ' + result); | |
console.log(decipher.output.toString()); | |
return decipher.output.toString(); | |
} | |
}); | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = require('express')(); | |
var http = require('http').Server(app); | |
var io = require('socket.io')(http); | |
var port = process.env.PORT || 3000; | |
var forge = require('node-forge'); | |
app.get('/', function(req, res){ | |
res.sendFile(__dirname + '/index.html'); | |
}); | |
io.on('connection', function(socket){ | |
// generate a random key and IV | |
// Note: a key size of 16 bytes will use AES-128, 24 => AES-192, 32 => AES-256 | |
var key = forge.random.getBytesSync(32); | |
var iv = forge.random.getBytesSync(16); | |
io.emit('symetric_key', {'key': key, 'iv': iv}); | |
function encrypt (bytesToEncrypt){ | |
cipher = forge.cipher.createCipher('AES-CBC', key); | |
cipher.start({'iv': iv}); | |
cipher.update(forge.util.createBuffer(bytesToEncrypt)); | |
cipher.finish(); | |
var encrypted = cipher.output; | |
console.log(encrypted.toHex()); | |
return encrypted | |
} | |
function decrypt (bytesToDecrypt){ | |
decipher = forge.cipher.createDecipher('AES-CBC', key); | |
decipher.start({'iv': iv}); | |
decipher.update(bytesToDecrypt); | |
var result = decipher.finish(); // check 'result' for true/false | |
console.log('decryption result = ' + result); | |
console.log(decipher.output.toString()); | |
return decipher.output.toString(); | |
} | |
socket.on('chat message', function(msg){ | |
//this is where the error happens | |
decrypt(msg) | |
//never executed | |
io.emit('chat message', encrypt("ola mundo")); | |
}); | |
}); | |
http.listen(port, function(){ | |
console.log('listening on *:' + port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment