Skip to content

Commit 1033040

Browse files
committed
Merge branch 'feature/crypto-secure-random' into develop
2 parents 3b4c51f + 7f809c9 commit 1033040

File tree

1 file changed

+41
-19
lines changed

1 file changed

+41
-19
lines changed

src/core.js

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,55 @@
1-
/*globals window, global*/
1+
/*globals window, global, require*/
22

33
/**
44
* CryptoJS core components.
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
*/
13-
var secureRandom = function () {
14-
// Native crypto module on NodeJS environment
15-
try {
16-
// Crypto from global object
17-
var crypto = global.crypto;
18-
19-
// Create a random float number between 0 and 1
20-
return Number('0.' + crypto.randomBytes(3).readUIntBE(0, 3));
21-
} catch (err) {}
22-
23-
// Native crypto module in Browser environment
24-
try {
25-
// Support experimental crypto module in IE 11
26-
var crypto = window.crypto || window.msCrypto;
37+
var cryptoSecureRandomInt = function () {
38+
if (crypto) {
39+
// Use getRandomValues method (Browser)
40+
if (typeof crypto.getRandomValues === 'function') {
41+
try {
42+
return crypto.getRandomValues(new Uint32Array(1))[0];
43+
} catch (err) {}
44+
}
2745

28-
// Create a random float number between 0 and 1
29-
return Number('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]);
30-
} catch (err) {}
46+
// Use randomBytes method (NodeJS)
47+
if (typeof crypto.randomBytes === 'function') {
48+
try {
49+
return crypto.randomBytes(4).readInt32LE();
50+
} catch (err) {}
51+
}
52+
}
3153

3254
throw new Error('Native crypto module could not be used to get secure random number.');
3355
};
@@ -321,7 +343,7 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
321343
var words = [];
322344

323345
for (var i = 0; i < nBytes; i += 4) {
324-
words.push((secureRandom() * 0x100000000) | 0);
346+
words.push(cryptoSecureRandomInt());
325347
}
326348

327349
return new WordArray.init(words, nBytes);

0 commit comments

Comments
 (0)