From 13268f9951fdf1c0f5fc32230286190026746485 Mon Sep 17 00:00:00 2001 From: paweldomas Date: Tue, 27 Oct 2015 14:34:10 -0500 Subject: [PATCH] Creates RandomUtil class for generating random strings. --- modules/util/RandomUtil.js | 37 ++++++++++++++++++++++++++++ modules/xmpp/LocalSSRCReplacement.js | 30 +++------------------- 2 files changed, 40 insertions(+), 27 deletions(-) create mode 100644 modules/util/RandomUtil.js diff --git a/modules/util/RandomUtil.js b/modules/util/RandomUtil.js new file mode 100644 index 000000000..ba888b020 --- /dev/null +++ b/modules/util/RandomUtil.js @@ -0,0 +1,37 @@ + +/** + * Generates random hex number within the range [min, max] + * @param max the maximum value for the generated number + * @param min the minimum value for the generated number + * @returns random hex number + */ +function rangeRandomHex(min, max) +{ + return Math.floor(Math.random() * (max - min) + min).toString(16); +} + +/** + * Exported interface. + */ +var RandomUtil = { + /** + * Generates hex number with length 4 + */ + random4digitsHex: function() { + return rangeRandomHex(4096, 65535); + }, + /** + * Generates hex number with length 8 + */ + random8digitsHex: function() { + return rangeRandomHex(268435456, 4294967295); + }, + /** + * Generates hex number with length 12 + */ + random12digitsHex: function() { + return rangeRandomHex(17592186044416, 281474976710655); + } +}; + +module.exports = RandomUtil; diff --git a/modules/xmpp/LocalSSRCReplacement.js b/modules/xmpp/LocalSSRCReplacement.js index 274c4229d..6f74a8c52 100644 --- a/modules/xmpp/LocalSSRCReplacement.js +++ b/modules/xmpp/LocalSSRCReplacement.js @@ -32,6 +32,7 @@ */ var SDP = require('./SDP'); +var RandomUtil = require('../util/RandomUtil'); var RTCBrowserType = require('../RTC/RTCBrowserType'); /** @@ -124,38 +125,13 @@ var storeLocalVideoSSRC = function (jingleIq) { }); }; -/** - * Generates random hex number within the range [min, max] - * @param max the maximum value for the generated number - * @param min the minimum value for the generated number - * @returns random hex number - */ -function rangeRandomHex(min, max) -{ - return Math.floor(Math.random() * (max - min) + min).toString(16); -} - -/** - * Generates hex number with length 4 - */ -var random4digitsHex = rangeRandomHex.bind(null, 4096, 65535); - -/** - * Generates hex number with length 8 - */ -var random8digitsHex = rangeRandomHex.bind(null, 268435456, 4294967295); - -/** - * Generates hex number with length 12 - */ -var random12digitsHex = rangeRandomHex.bind(null, 17592186044416, 281474976710655); - /** * Generates new label/mslabel attribute * @returns {string} label/mslabel attribute */ function generateLabel() { - return random8digitsHex() + "-" + random4digitsHex() + "-" + random4digitsHex() + "-" + random4digitsHex() + "-" + random12digitsHex(); + return RandomUtil.random8digitsHex() + "-" + RandomUtil.random4digitsHex() + "-" + + RandomUtil.random4digitsHex() + "-" + RandomUtil.random4digitsHex() + "-" + RandomUtil.random12digitsHex(); } /**