Skip to content

Commit e4ac157

Browse files
committed
Do not convert into float number.
1 parent 3b4c51f commit e4ac157

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/core.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,21 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
1010
*
1111
* As Math.random() is cryptographically not safe to use
1212
*/
13-
var secureRandom = function () {
13+
var cryptoSecureRandomInt = function () {
1414
// Native crypto module on NodeJS environment
1515
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');
1818

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();
2120
} catch (err) {}
2221

2322
// Native crypto module in Browser environment
2423
try {
2524
// Support experimental crypto module in IE 11
2625
var crypto = window.crypto || window.msCrypto;
2726

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;
3028
} catch (err) {}
3129

3230
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) {
321319
var words = [];
322320

323321
for (var i = 0; i < nBytes; i += 4) {
324-
words.push((secureRandom() * 0x100000000) | 0);
322+
words.push((cryptoSecureRandomInt());
325323
}
326324

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

0 commit comments

Comments
 (0)