aboutsummaryrefslogtreecommitdiff
path: root/node_modules/brorand/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'node_modules/brorand/index.js')
-rw-r--r--node_modules/brorand/index.js65
1 files changed, 65 insertions, 0 deletions
diff --git a/node_modules/brorand/index.js b/node_modules/brorand/index.js
new file mode 100644
index 000000000..9a0fff4d2
--- /dev/null
+++ b/node_modules/brorand/index.js
@@ -0,0 +1,65 @@
+var r;
+
+module.exports = function rand(len) {
+ if (!r)
+ r = new Rand(null);
+
+ return r.generate(len);
+};
+
+function Rand(rand) {
+ this.rand = rand;
+}
+module.exports.Rand = Rand;
+
+Rand.prototype.generate = function generate(len) {
+ return this._rand(len);
+};
+
+// Emulate crypto API using randy
+Rand.prototype._rand = function _rand(n) {
+ if (this.rand.getBytes)
+ return this.rand.getBytes(n);
+
+ var res = new Uint8Array(n);
+ for (var i = 0; i < res.length; i++)
+ res[i] = this.rand.getByte();
+ return res;
+};
+
+if (typeof self === 'object') {
+ if (self.crypto && self.crypto.getRandomValues) {
+ // Modern browsers
+ Rand.prototype._rand = function _rand(n) {
+ var arr = new Uint8Array(n);
+ self.crypto.getRandomValues(arr);
+ return arr;
+ };
+ } else if (self.msCrypto && self.msCrypto.getRandomValues) {
+ // IE
+ Rand.prototype._rand = function _rand(n) {
+ var arr = new Uint8Array(n);
+ self.msCrypto.getRandomValues(arr);
+ return arr;
+ };
+
+ // Safari's WebWorkers do not have `crypto`
+ } else if (typeof window === 'object') {
+ // Old junk
+ Rand.prototype._rand = function() {
+ throw new Error('Not implemented yet');
+ };
+ }
+} else {
+ // Node.js or Web worker with no crypto support
+ try {
+ var crypto = require('crypto');
+ if (typeof crypto.randomBytes !== 'function')
+ throw new Error('Not supported');
+
+ Rand.prototype._rand = function _rand(n) {
+ return crypto.randomBytes(n);
+ };
+ } catch (e) {
+ }
+}