浏览代码

Creates RandomUtil class for generating random strings.

release-8443
paweldomas 10 年前
父节点
当前提交
a96fd484f8
共有 2 个文件被更改,包括 40 次插入27 次删除
  1. 37
    0
      modules/util/RandomUtil.js
  2. 3
    27
      modules/xmpp/LocalSSRCReplacement.js

+ 37
- 0
modules/util/RandomUtil.js 查看文件

@@ -0,0 +1,37 @@
1
+
2
+/**
3
+ * Generates random hex number within the range [min, max]
4
+ * @param max the maximum value for the generated number
5
+ * @param min the minimum value for the generated number
6
+ * @returns random hex number
7
+ */
8
+function rangeRandomHex(min, max)
9
+{
10
+    return Math.floor(Math.random() * (max - min) + min).toString(16);
11
+}
12
+
13
+/**
14
+ * Exported interface.
15
+ */
16
+var RandomUtil = {
17
+    /**
18
+     * Generates hex number with length 4
19
+     */
20
+    random4digitsHex: function() {
21
+        return rangeRandomHex(4096, 65535);
22
+    },
23
+    /**
24
+     * Generates hex number with length 8
25
+     */
26
+    random8digitsHex: function() {
27
+        return rangeRandomHex(268435456, 4294967295);
28
+    },
29
+    /**
30
+     * Generates hex number with length 12
31
+     */
32
+    random12digitsHex: function() {
33
+        return rangeRandomHex(17592186044416, 281474976710655);
34
+    }
35
+};
36
+
37
+module.exports = RandomUtil;

+ 3
- 27
modules/xmpp/LocalSSRCReplacement.js 查看文件

@@ -32,6 +32,7 @@
32 32
  */
33 33
 
34 34
 var SDP = require('./SDP');
35
+var RandomUtil = require('../util/RandomUtil');
35 36
 var RTCBrowserType = require('../RTC/RTCBrowserType');
36 37
 
37 38
 /**
@@ -124,38 +125,13 @@ var storeLocalVideoSSRC = function (jingleIq) {
124 125
     });
125 126
 };
126 127
 
127
-/**
128
- * Generates random hex number within the range [min, max]
129
- * @param max the maximum value for the generated number
130
- * @param min the minimum value for the generated number
131
- * @returns random hex number
132
- */
133
-function rangeRandomHex(min, max)
134
-{
135
-    return Math.floor(Math.random() * (max - min) + min).toString(16);
136
-}
137
-
138
-/**
139
- * Generates hex number with length 4
140
- */
141
-var random4digitsHex = rangeRandomHex.bind(null, 4096, 65535);
142
-
143
-/**
144
- * Generates hex number with length 8
145
- */
146
-var random8digitsHex = rangeRandomHex.bind(null, 268435456, 4294967295);
147
-
148
-/**
149
- * Generates hex number with length 12
150
- */
151
-var random12digitsHex = rangeRandomHex.bind(null, 17592186044416, 281474976710655);
152
-
153 128
 /**
154 129
  * Generates new label/mslabel attribute
155 130
  * @returns {string} label/mslabel attribute
156 131
  */
157 132
 function generateLabel() {
158
-    return random8digitsHex() + "-" + random4digitsHex() + "-" + random4digitsHex() + "-" + random4digitsHex() + "-" + random12digitsHex();
133
+    return RandomUtil.random8digitsHex() + "-" + RandomUtil.random4digitsHex() + "-" +
134
+           RandomUtil.random4digitsHex() + "-" + RandomUtil.random4digitsHex() + "-" + RandomUtil.random12digitsHex();
159 135
 }
160 136
 
161 137
 /**

正在加载...
取消
保存