From 58b6dee87a15cf79f16d1f45d3777cc4f4fc3c92 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Fri, 25 Nov 2016 11:46:17 +0000 Subject: [PATCH 1/4] use crypto random numbers --- node_modules/kaefer/lib/server.js | 2 +- node_modules/kaefer/lib/util.js | 27 +++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/node_modules/kaefer/lib/server.js b/node_modules/kaefer/lib/server.js index 1337242a..bd3de993 100644 --- a/node_modules/kaefer/lib/server.js +++ b/node_modules/kaefer/lib/server.js @@ -78,7 +78,7 @@ Server.prototype.getConnection = function(id) { } } - id = util.uid(); + id = util.uid(10); transport = this.sockets[id] = new ReliableSocket(new ReconnectSocket(), this.socketOptions); transport.on("disconnect", this.disconnect.bind(this, transport)); transport.id = id; diff --git a/node_modules/kaefer/lib/util.js b/node_modules/kaefer/lib/util.js index beefc319..df3940f0 100644 --- a/node_modules/kaefer/lib/util.js +++ b/node_modules/kaefer/lib/util.js @@ -4,8 +4,31 @@ exports.inherits = function(Child, Parent) { Child.prototype = Object.create(Parent.prototype, { constructor: { value: Child }}); } -exports.uid = function() { - return (Date.now() + Math.random() * 0x100000000).toString(36) + (Math.random() * 0x100000000).toString(36); +var isNode = (typeof global !== "undefined" && ({}).toString.call(global) == '[object global]'); +var isBrowser = (typeof window !== "undefined" && ({}).toString.call(window) == '[object window]'); + +if (isNode) { + exports.uid = function(length) { + return (require("crypto") + .randomBytes(length) + .toString("base64") + .slice(0, length) + ); + }; +} else if (isBrowser && window.crypto && window.crypto.getRandomValues) { + exports.uid = function(length) { + var values = new Uint8Array(length); + window.crypto.getRandomValues(values); + return btoa(String.fromCharCode.apply(null, values)).slice(0, 10); + }; +} else { + exports.uid = function(length) { + var arr = []; + for (var i = 0; i < length; i++) { + arr[i] = Math.floor(Math.random() * 255); + } + return btoa(String.fromCharCode.apply(null, values)).slice(0, 10); + }; } }); \ No newline at end of file From bf63ab1ec6cf5f3f9f84fc152a5c01eaed2c9d9e Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Fri, 25 Nov 2016 13:08:14 +0000 Subject: [PATCH 2/4] make sure packager doesn't get confused --- node_modules/kaefer/lib/util.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/node_modules/kaefer/lib/util.js b/node_modules/kaefer/lib/util.js index df3940f0..06f781b2 100644 --- a/node_modules/kaefer/lib/util.js +++ b/node_modules/kaefer/lib/util.js @@ -2,14 +2,15 @@ define(function(require, exports, module) { exports.inherits = function(Child, Parent) { Child.prototype = Object.create(Parent.prototype, { constructor: { value: Child }}); -} +}; var isNode = (typeof global !== "undefined" && ({}).toString.call(global) == '[object global]'); var isBrowser = (typeof window !== "undefined" && ({}).toString.call(window) == '[object window]'); if (isNode) { exports.uid = function(length) { - return (require("crypto") + // make sure packagers don't try to package "crypto" + return (require("cry" + "pto") .randomBytes(length) .toString("base64") .slice(0, length) @@ -27,7 +28,7 @@ if (isNode) { for (var i = 0; i < length; i++) { arr[i] = Math.floor(Math.random() * 255); } - return btoa(String.fromCharCode.apply(null, values)).slice(0, 10); + return btoa(String.fromCharCode.apply(null, arr)).slice(0, 10); }; } From 181b61e5b65a92e423cafceb638a3b51d6ea9e99 Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Tue, 29 Nov 2016 16:13:11 -0800 Subject: [PATCH 3/4] use length iso 10 --- node_modules/kaefer/lib/util.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/node_modules/kaefer/lib/util.js b/node_modules/kaefer/lib/util.js index 06f781b2..772faba7 100644 --- a/node_modules/kaefer/lib/util.js +++ b/node_modules/kaefer/lib/util.js @@ -20,7 +20,7 @@ if (isNode) { exports.uid = function(length) { var values = new Uint8Array(length); window.crypto.getRandomValues(values); - return btoa(String.fromCharCode.apply(null, values)).slice(0, 10); + return btoa(String.fromCharCode.apply(null, values)).slice(0, length); }; } else { exports.uid = function(length) { @@ -28,8 +28,8 @@ if (isNode) { for (var i = 0; i < length; i++) { arr[i] = Math.floor(Math.random() * 255); } - return btoa(String.fromCharCode.apply(null, arr)).slice(0, 10); + return btoa(String.fromCharCode.apply(null, arr)).slice(0, length); }; } -}); \ No newline at end of file +}); From 294d2acb6f1f1bf58ed270b9f2efbca548b8c29b Mon Sep 17 00:00:00 2001 From: Fabian Jakobs Date: Tue, 20 Dec 2016 13:55:19 +0000 Subject: [PATCH 4/4] generating uid is only needed on the server --- node_modules/kaefer/lib/server.js | 13 +++++++++++-- node_modules/kaefer/lib/util.js | 28 ---------------------------- 2 files changed, 11 insertions(+), 30 deletions(-) diff --git a/node_modules/kaefer/lib/server.js b/node_modules/kaefer/lib/server.js index bd3de993..9dcbe169 100644 --- a/node_modules/kaefer/lib/server.js +++ b/node_modules/kaefer/lib/server.js @@ -22,6 +22,7 @@ var util = require("./util"); var EventEmitter = require("events").EventEmitter; +var crypto = require("crypto"); var ReliableSocket = require("./reliable_socket"); var ReconnectSocket = require("./reconnect_socket"); @@ -78,7 +79,7 @@ Server.prototype.getConnection = function(id) { } } - id = util.uid(10); + id = generateUid(10); transport = this.sockets[id] = new ReliableSocket(new ReconnectSocket(), this.socketOptions); transport.on("disconnect", this.disconnect.bind(this, transport)); transport.id = id; @@ -92,4 +93,12 @@ Server.prototype.disconnect = function(transport) { transport.close(); this.emit("disconnect", transport); } -}; \ No newline at end of file +}; + +function generateUid(length) { + return (crypto + .randomBytes(length) + .toString("base64") + .slice(0, length) + ); +} diff --git a/node_modules/kaefer/lib/util.js b/node_modules/kaefer/lib/util.js index 772faba7..3c9c5784 100644 --- a/node_modules/kaefer/lib/util.js +++ b/node_modules/kaefer/lib/util.js @@ -4,32 +4,4 @@ exports.inherits = function(Child, Parent) { Child.prototype = Object.create(Parent.prototype, { constructor: { value: Child }}); }; -var isNode = (typeof global !== "undefined" && ({}).toString.call(global) == '[object global]'); -var isBrowser = (typeof window !== "undefined" && ({}).toString.call(window) == '[object window]'); - -if (isNode) { - exports.uid = function(length) { - // make sure packagers don't try to package "crypto" - return (require("cry" + "pto") - .randomBytes(length) - .toString("base64") - .slice(0, length) - ); - }; -} else if (isBrowser && window.crypto && window.crypto.getRandomValues) { - exports.uid = function(length) { - var values = new Uint8Array(length); - window.crypto.getRandomValues(values); - return btoa(String.fromCharCode.apply(null, values)).slice(0, length); - }; -} else { - exports.uid = function(length) { - var arr = []; - for (var i = 0; i < length; i++) { - arr[i] = Math.floor(Math.random() * 255); - } - return btoa(String.fromCharCode.apply(null, arr)).slice(0, length); - }; -} - });