@@ -10,23 +10,21 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
10
10
*
11
11
* As Math.random() is cryptographically not safe to use
12
12
*/
13
- var secureRandom = function ( ) {
13
+ var cryptoSecureRandomInt = function ( ) {
14
14
// Native crypto module on NodeJS environment
15
15
try {
16
- // Crypto from global object
17
- var crypto = global . crypto ;
16
+ // Native rypto from global object or import via require
17
+ var crypto = global . crypto || require ( 'crypto' ) ;
18
18
19
- // Create a random float number between 0 and 1
20
- return Number ( '0.' + crypto . randomBytes ( 3 ) . readUIntBE ( 0 , 3 ) ) ;
19
+ return crypto . randomBytes ( 4 ) . readInt32LE ( ) ;
21
20
} catch ( err ) { }
22
21
23
22
// Native crypto module in Browser environment
24
23
try {
25
24
// Support experimental crypto module in IE 11
26
25
var crypto = window . crypto || window . msCrypto ;
27
26
28
- // Create a random float number between 0 and 1
29
- return Number ( '0.' + window . crypto . getRandomValues ( new Uint32Array ( 1 ) ) [ 0 ] ) ;
27
+ return ( crypto . getRandomValues ( new Uint32Array ( 1 ) ) [ 0 ] ) | 1 ;
30
28
} catch ( err ) { }
31
29
32
30
throw new Error ( 'Native crypto module could not be used to get secure random number.' ) ;
@@ -321,7 +319,7 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
321
319
var words = [ ] ;
322
320
323
321
for ( var i = 0 ; i < nBytes ; i += 4 ) {
324
- words . push ( ( secureRandom ( ) * 0x100000000 ) | 0 ) ;
322
+ words . push ( ( cryptoSecureRandomInt ( ) ) ;
325
323
}
326
324
327
325
return new WordArray . init ( words , nBytes ) ;
0 commit comments