Use TripleSec Encryption with Node.js Socket.io Chat

I am new to Node.js and I created a chat application using Socket.io. I am trying to encrypt messages using the triplesec library, but I am having some problems. What would be the best approach to add this encryption / decryption:

var triplesec = require('triplesec');

// Encrypt Function
triplesec.encrypt({
    key: new triplesec.Buffer('secretkey'),
    data: new triplesec.Buffer('secretthings'),
}, function (err, buff) {
    if(!err) {
        var ciphertext = buff.toString('hex')
        console.log(buff.toString('hex'))
    }

    // Decrypt Function
    triplesec.decrypt({
        data: new triplesec.Buffer(ciphertext, "hex"),
        key: new triplesec.Buffer('secretkey')
    }, function (err, buff) {
        if(!err) {
            console.log(buff.toString());
        }
    });

});

To this client: (All encryption of messages coming and going will be processed on the client side, if we assume that this is the best approach?)

// imports
var readline = require('readline');
var socketio = require('socket.io-client');
var util = require('util');
var clc = require("cli-color");
var async = require("async");
// globals
var nick;
var serverAddress;
var serverPort;
var socket;
var rl = readline.createInterface(process.stdin, process.stdout);
// message types
var chat = clc.green;
var pm = clc.yellow;
var notice = clc.cyan;
var emote = clc.blue;
var error = clc.red;

// function definitions
function consoleOut (msg) {
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
    console.log(msg);
    rl.prompt(true);
}

// handle a command that the user has entered
function handleCommand (commandType, arg) {
    switch (commandType) {
        case 'nick': // set the nickname and send a message with the updated nickname
            var notice = nick + " changed their name to " + arg;
            nick = arg;
            socket.emit('send', { type: 'notice', message: notice });
            break;
        case 'pm': // private message another user
            var to = arg.match(/[a-z]+\b/)[0];
            var message = arg.substr(to.length, arg.length);
            socket.emit('send', { type: 'pm', message: message, to: to, from: nick });
            break;

        case 'me': // the user performs some emote
            var emote = nick + " " + arg;
            socket.emit('send', { type: 'emote', message: emote });
            break;
        default: // invalid command type
            consoleOut(error("That is not a valid command."));
    }
}

// start of execution
async.series([
    function(callback) {
        // get the address
        rl.question("Please enter the address of the server, such as 192.168.0.10: ", function(address) {
            serverAddress = address;
            callback();
        });
    },
    function(callback) {
        // get the port
        rl.question("Please enter the port the server is listening on, such as 8080: ", function(port) {
            serverPort = port;
            socket = socketio.connect('http://' + serverAddress + ':' + serverPort);
            // register the sockets on message event handler
            socket.on('message', function (data) {
                var leader;
                // process message, these are pretty self explainitory
                if (data.type == 'chat' && data.nick != nick) {
                    leader = chat("<" + data.nick + "> ");
                    consoleOut(leader + data.message);
                }
                else if (data.type == "notice") {
                    consoleOut(notice(data.message));
                }
                else if (data.type == "pm" && data.to == nick) {
                    leader = pm("["+data.from+"->"+data.to+"]");
                    consoleOut(leader + data.message);
                }
                else if (data.type == "emote") {
                    consoleOut(emote(data.message));
                }
            });
            callback();
        });
    },
    function(callback) {
        // get the users nickname
        rl.question("Please enter a nickname: ", function(name) {
            nick = name;
            var msg = nick + " has joined the chat";
            socket.emit('send', { type: 'notice', message: msg });
            rl.prompt(true);
            callback();
        });
    }
]);

// called when the user hits enter on the command line
// parses what ever they typed into either a command or a chat message
rl.on('line', function (line) {
    if (line[0] == "/" && line.length > 1) {
        var cmd = line.match(/[a-z]+\b/)[0];
        var arg = line.substr(cmd.length+2, line.length);
        handleCommand(cmd, arg);
        rl.prompt(true);
    } else {
        // send chat message
        socket.emit('send', { type: 'chat', message: line, nick: nick });
        rl.prompt(true);
    }
});
+4
source share

Source: https://habr.com/ru/post/1616995/


All Articles