Skip to content

Commit

Permalink
Added encryption option, and deployed new version to npm
Browse files Browse the repository at this point in the history
  • Loading branch information
digitalwm committed Feb 24, 2012
1 parent 5bf38a9 commit f03756e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
46 changes: 42 additions & 4 deletions cloudjs.js
Expand Up @@ -17,12 +17,15 @@
timeout : int,
balance : int,
heartbeat : int
hasEncryption : bool,
encryptionKey : string
}
*/

//noinspection JSUnresolvedVariable
var util = require('util'),
events = require('events').EventEmitter,
crypto = require('cryptojs').Crypto,
dgram = require('dgram'),
serializer = require("JASON"),
totalOps = 0, totalTime = 0;
Expand Down Expand Up @@ -313,7 +316,9 @@ function Clouder(port, group, config) {
var hasPool = false,
timeout = 5000,
balance = 10000,
heartbeat = 2000;
heartbeat = 2000,
hasEncryption = false,
encryptionKey = '12345';

if(typeof(config) !== "undefined") {
//set config values
Expand All @@ -329,12 +334,20 @@ function Clouder(port, group, config) {
if(typeof(config.heartbeat) === 'number') {
heartbeat = config.heartbeat;
}
if(typeof(config.hasEncryption) === 'boolean') {
hasEncryption = config.hasEncryption;
}
if(typeof(config.encryptionKey) === 'string') {
encryptionKey = config.encryptionKey;
}
}

this._hasPool = hasPool;
this._timeout = timeout;
this._balance = balance;
this._heartbeat = heartbeat;
this._hasEncription = hasEncryption;
this._encryptionKey = encryptionKey;
this.id = guidGenerator();
this.port = port;
this.group = group;
Expand Down Expand Up @@ -423,8 +436,15 @@ Clouder.prototype.connect = function () {

//noinspection JSUnresolvedFunction
this.socket.on('message', function (buf) {
var msg, bodyParser;
var msg, bodyParser,
mode, dataBytes, dataDecripted;
try {
if(self._hasEncription === true) {
mode = new crypto.mode.ECB(crypto.pad.pkcs7);
dataBytes = crypto.util.hexToBytes(buf.toString());
dataDecripted = crypto.DES.decrypt(dataBytes, self._encryptionKey, {asBytes: true, mode: mode});
buf = crypto.charenc.UTF8.bytesToString(dataDecripted);
}
msg = serializer.parse(buf);
if(typeof(msg.type) === 'undefined' || typeof(msg.title) === 'undefined' || typeof(msg.body) === 'undefined') {
return;
Expand Down Expand Up @@ -462,6 +482,7 @@ Clouder.prototype.connect = function () {
});

this.sendOperational = function (title, message, bounce) {
var mode, dataBytes, dataEncripted;
if(typeof(bounce) === 'undefined') {
bounce = true;
}
Expand All @@ -473,7 +494,15 @@ Clouder.prototype.connect = function () {
body : message
};
try {
messageBuffer = new Buffer(serializer.stringify(msg));
if(this._hasEncription === true) {
mode = new crypto.mode.ECB(crypto.pad.pkcs7);
dataBytes = crypto.charenc.UTF8.stringToBytes(serializer.stringify(msg));
dataEncripted = crypto.DES.encrypt(dataBytes, this._encryptionKey, {asBytes: true, mode: mode});
messageBuffer = new Buffer(crypto.util.bytesToHex(dataEncripted).toString());
}
else {
messageBuffer = new Buffer(serializer.stringify(msg));
}
}
catch(Exception) {
throw "Message to complex to be sent";
Expand All @@ -482,6 +511,7 @@ Clouder.prototype.connect = function () {
};

this.send = function (title, message, self) {
var mode, dataBytes, dataEncripted;
if(typeof(self) === 'undefined') {
self = false;
}
Expand All @@ -494,7 +524,15 @@ Clouder.prototype.connect = function () {
body : message
};
try {
messageBuffer = new Buffer(serializer.stringify(msg));
if(this._hasEncription === true) {
mode = new crypto.mode.ECB(crypto.pad.pkcs7);
dataBytes = crypto.charenc.UTF8.stringToBytes(serializer.stringify(msg));
dataEncripted = crypto.DES.encrypt(dataBytes, this._encryptionKey, {asBytes: true, mode: mode});
messageBuffer = new Buffer(crypto.util.bytesToHex(dataEncripted));
}
else {
messageBuffer = new Buffer(serializer.stringify(msg));
}
}
catch(Exception) {
throw "Message to complex to be sent";
Expand Down
8 changes: 5 additions & 3 deletions example.js
Expand Up @@ -12,9 +12,11 @@ var cloud,
cloud = require("./cloudjs.js");

cloudConfig = {
hasPool : true,
timeout : 3000,
balance : 20000
hasPool : true,
timeout : 3000,
balance : 20000,
hasEncryption : true,
encryptionKey : '12345'
};

var myObject = new cloud.Clouder(11211, "255.255.255.255", cloudConfig);
Expand Down
11 changes: 6 additions & 5 deletions package.json
Expand Up @@ -2,18 +2,19 @@
"author" : "Dan Harabagiu <harabagiu.dan@gmail.com> (http://dan.harabagiu.net/)",
"name" : "cloudjs",
"description" : "Making from nodeJS a cloud distributed event system",
"version" : "0.0.3",
"version" : "0.0.4",
"homepage" : "http://dan.harabagiu.net/cloudjs",
"keywords" : ["udp", "broadcast", "realtime", "cloud", "event"],
"repository" : {
"type" : "git",
"url" : "git@github.com:digitalwm/cloudjs.git"
"type" : "git",
"url" : "git@github.com:digitalwm/cloudjs.git"
},
"main" : "cloudjs.js",
"engines" : {
"node" : "0.6*"
"node" : "0.6*"
},
"dependencies" : {
"JASON" : "0.1.2"
"JASON" : "0.1.2",
"cryptojs" : "2.5.3"
}
}

0 comments on commit f03756e

Please sign in to comment.