use crypto random numbers

pull/385/head
Fabian Jakobs 2016-11-25 11:46:17 +00:00
rodzic b315a23f74
commit 58b6dee87a
2 zmienionych plików z 26 dodań i 3 usunięć

2
node_modules/kaefer/lib/server.js wygenerowano vendored
Wyświetl plik

@ -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;

27
node_modules/kaefer/lib/util.js wygenerowano vendored
Wyświetl plik

@ -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);
};
}
});