Skip to content

Commit 7f809c9

Browse files
committed
Do not run the detect native crypto module for every cryptoSecureRandomInt call.
1 parent ac28862 commit 7f809c9

File tree

1 file changed

+24
-24
lines changed

1 file changed

+24
-24
lines changed

src/core.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,36 @@
55
*/
66
var CryptoJS = CryptoJS || (function (Math, undefined) {
77

8+
var crypto;
9+
10+
// Native crypto from window (Browser)
11+
if (typeof window !== 'undefined' && window.crypto) {
12+
crypto = window.crypto;
13+
}
14+
15+
// Native (experimental IE 11) crypto from window (Browser)
16+
if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
17+
crypto = window.msCrypto;
18+
}
19+
20+
// Native crypto from global (NodeJS)
21+
if (!crypto && typeof global !== 'undefined' && global.crypto) {
22+
crypto = global.crypto;
23+
}
24+
25+
// Native crypto import via require (NodeJS)
26+
if (!crypto && typeof require === 'function') {
27+
try {
28+
crypto = require('crypto');
29+
} catch (err) {}
30+
}
31+
832
/*
933
* Cryptographically secure pseudorandom number generator
1034
*
1135
* As Math.random() is cryptographically not safe to use
1236
*/
1337
var cryptoSecureRandomInt = function () {
14-
var crypto;
15-
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 {
34-
crypto = require('crypto');
35-
} catch (err) {}
36-
}
37-
3838
if (crypto) {
3939
// Use getRandomValues method (Browser)
4040
if (typeof crypto.getRandomValues === 'function') {

0 commit comments

Comments
 (0)