Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80542 views
1
'use strict';
2
3
var crypto = global.crypto || global.msCrypto
4
if(crypto && crypto.getRandomValues) {
5
module.exports = randomBytes;
6
} else {
7
module.exports = oldBrowser;
8
}
9
function randomBytes(size, cb) {
10
var bytes = new Buffer(size); //in browserify, this is an extended Uint8Array
11
/* This will not work in older browsers.
12
* See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
13
*/
14
15
crypto.getRandomValues(bytes);
16
if (typeof cb === 'function') {
17
return process.nextTick(function () {
18
cb(null, bytes);
19
});
20
}
21
return bytes;
22
}
23
function oldBrowser() {
24
throw new Error(
25
'secure random number generation not supported by this browser\n'+
26
'use chrome, FireFox or Internet Explorer 11'
27
)
28
}
29
30