mirror of
https://github.com/wu736139669/Monkey-Test.git
synced 2026-08-06 06:14:53 +00:00
27 lines
800 B
JavaScript
27 lines
800 B
JavaScript
(function() {
|
|
var g = ('undefined' === typeof window ? global : window) || {}
|
|
_crypto = (
|
|
g.crypto || g.msCrypto || require('crypto')
|
|
)
|
|
module.exports = function(size) {
|
|
// Modern Browsers
|
|
if(_crypto.getRandomValues) {
|
|
var bytes = new Buffer(size); //in browserify, this is an extended Uint8Array
|
|
/* This will not work in older browsers.
|
|
* See https://developer.mozilla.org/en-US/docs/Web/API/window.crypto.getRandomValues
|
|
*/
|
|
|
|
_crypto.getRandomValues(bytes);
|
|
return bytes;
|
|
}
|
|
else if (_crypto.randomBytes) {
|
|
return _crypto.randomBytes(size)
|
|
}
|
|
else
|
|
throw new Error(
|
|
'secure random number generation not supported by this browser\n'+
|
|
'use chrome, FireFox or Internet Explorer 11'
|
|
)
|
|
}
|
|
}())
|