Skip to content
Browse files

Added encryption option, and deployed new version to npm

  • Loading branch information...
1 parent 5bf38a9 commit f03756e8fb926c477fc0455e5d4d9ea6453ae518 @digitalwm committed Feb 24, 2012
Showing with 53 additions and 12 deletions.
  1. +42 −4 cloudjs.js
  2. +5 −3 example.js
  3. +6 −5 package.json
View
46 cloudjs.js
@@ -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;
@@ -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
@@ -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;
@@ -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;
@@ -462,6 +482,7 @@ Clouder.prototype.connect = function () {
});
this.sendOperational = function (title, message, bounce) {
+ var mode, dataBytes, dataEncripted;
if(typeof(bounce) === 'undefined') {
bounce = true;
}
@@ -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";
@@ -482,6 +511,7 @@ Clouder.prototype.connect = function () {
};
this.send = function (title, message, self) {
+ var mode, dataBytes, dataEncripted;
if(typeof(self) === 'undefined') {
self = false;
}
@@ -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";
View
8 example.js
@@ -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);
View
11 package.json
@@ -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.
Something went wrong with that request. Please try again.