1
- /*globals window, global*/
1
+ /*globals window, global, require */
2
2
3
3
/**
4
4
* CryptoJS core components.
@@ -13,39 +13,37 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
13
13
var cryptoSecureRandomInt = function ( ) {
14
14
var crypto ;
15
15
16
- // Native crypto module in Browser environment
17
- try {
18
- if ( typeof window !== 'undefined' ) {
19
- if ( window . crypto ) {
20
- // Use global crypto module
21
- crypto = window . crypto ;
22
- } else if ( window . msCrypto ) {
23
- // Support experimental crypto module in IE 11
24
- crypto = window . msCrypto ;
25
- }
26
- }
27
- } catch ( err ) { }
28
-
29
- // Native crypto module on NodeJS environment
30
- try {
31
- if ( typeof global !== 'undefined' && global . crypto ) {
32
- // Native crypto from global
33
- crypto = global . crypto ;
34
- } else if ( typeof require === 'function' ) {
35
- // Native crypto import via require
16
+ // Native crypto from window (Browser)
17
+ if ( typeof window !== 'undefined' && window . crypto ) {
18
+ crypto = window . crypto ;
19
+ }
20
+
21
+ // Native (experimental IE 11) crypto from window (Browser)
22
+ if ( ! crypto && typeof window !== 'undefined' && window . msCrypto ) {
23
+ crypto = window . msCrypto ;
24
+ }
25
+
26
+ // Native crypto from global (NodeJS)
27
+ if ( ! crypto && typeof global !== 'undefined' && global . crypto ) {
28
+ crypto = global . crypto ;
29
+ }
30
+
31
+ // Native crypto import via require (NodeJS)
32
+ if ( ! crypto && typeof require === 'function' ) {
33
+ try {
36
34
crypto = require ( 'crypto' ) ;
37
- }
38
- } catch ( err ) { }
35
+ } catch ( err ) { }
36
+ }
39
37
40
38
if ( crypto ) {
41
- // Use getRandomValues method
39
+ // Use getRandomValues method (Browser)
42
40
if ( typeof crypto . getRandomValues === 'function' ) {
43
41
try {
44
42
return crypto . getRandomValues ( new Uint32Array ( 1 ) ) [ 0 ] ;
45
43
} catch ( err ) { }
46
44
}
47
45
48
- // Use randomBytes method
46
+ // Use randomBytes method (NodeJS)
49
47
if ( typeof crypto . randomBytes === 'function' ) {
50
48
try {
51
49
return crypto . randomBytes ( 4 ) . readInt32LE ( ) ;
0 commit comments