Skip to content

Commit 79209bc

Browse files
committed
Merge branch 'release/3.2.1'
2 parents 6a6d99a + 78bde5f commit 79209bc

File tree

5 files changed

+111
-38
lines changed

5 files changed

+111
-38
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,28 @@ console.log(decryptedData); // [{id: 1}, {id: 2}]
208208
- ```crypto-js/pad-iso97971```
209209
- ```crypto-js/pad-zeropadding```
210210
- ```crypto-js/pad-nopadding```
211+
212+
213+
## Release notes
214+
215+
### 3.2.1
216+
217+
The usage of the native crypto module has been fixed. The import and access of the native crypto module has been improved.
218+
219+
### 3.2.0
220+
221+
In this version `Math.random()` has been replaced by the random methods of the native crypto module.
222+
223+
For this reason CryptoJS might does not run in some JavaScript environments without native crypto module. Such as IE 10 or before.
224+
225+
If it's absolute required to run CryptoJS in such an environment, stay with `3.1.x` version. Encrypting and decrypting stays compatible. But keep in mind `3.1.x` versions still use `Math.random()` which is cryptographically not secure, as it's not random enough.
226+
227+
This version came along with `CRITICAL` `BUG`.
228+
229+
DO NOT USE THIS VERSION! Please, go for a newer version!
230+
231+
### 3.1.x
232+
233+
The `3.1.x` are based on the original CryptoJS, wrapped in CommonJS modules.
234+
235+

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crypto-js",
3-
"version": "3.2.0",
3+
"version": "3.2.1",
44
"description": "JavaScript library of crypto standards.",
55
"license": "MIT",
66
"homepage": "http://github.com/brix/crypto-js",

core.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,58 @@
1313
}
1414
}(this, function () {
1515

16+
/*globals window, global, require*/
17+
1618
/**
1719
* CryptoJS core components.
1820
*/
1921
var CryptoJS = CryptoJS || (function (Math, undefined) {
2022

23+
var crypto;
24+
25+
// Native crypto from window (Browser)
26+
if (typeof window !== 'undefined' && window.crypto) {
27+
crypto = window.crypto;
28+
}
29+
30+
// Native (experimental IE 11) crypto from window (Browser)
31+
if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
32+
crypto = window.msCrypto;
33+
}
34+
35+
// Native crypto from global (NodeJS)
36+
if (!crypto && typeof global !== 'undefined' && global.crypto) {
37+
crypto = global.crypto;
38+
}
39+
40+
// Native crypto import via require (NodeJS)
41+
if (!crypto && typeof require === 'function') {
42+
try {
43+
crypto = require('crypto');
44+
} catch (err) {}
45+
}
46+
2147
/*
2248
* Cryptographically secure pseudorandom number generator
2349
*
2450
* As Math.random() is cryptographically not safe to use
2551
*/
26-
var secureRandom = function () {
27-
// Native crypto module on NodeJS environment
28-
try {
29-
// Crypto from global object
30-
var crypto = global.crypto;
31-
32-
// Create a random float number between 0 and 1
33-
return Number('0.' + crypto.randomBytes(3).readUIntBE(0, 3));
34-
} catch (err) {}
35-
36-
// Native crypto module in Browser environment
37-
try {
38-
// Support experimental crypto module in IE 11
39-
var crypto = window.crypto || window.msCrypto;
52+
var cryptoSecureRandomInt = function () {
53+
if (crypto) {
54+
// Use getRandomValues method (Browser)
55+
if (typeof crypto.getRandomValues === 'function') {
56+
try {
57+
return crypto.getRandomValues(new Uint32Array(1))[0];
58+
} catch (err) {}
59+
}
4060

41-
// Create a random float number between 0 and 1
42-
return Number('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]);
43-
} catch (err) {}
61+
// Use randomBytes method (NodeJS)
62+
if (typeof crypto.randomBytes === 'function') {
63+
try {
64+
return crypto.randomBytes(4).readInt32LE();
65+
} catch (err) {}
66+
}
67+
}
4468

4569
throw new Error('Native crypto module could not be used to get secure random number.');
4670
};
@@ -334,7 +358,7 @@
334358
var words = [];
335359

336360
for (var i = 0; i < nBytes; i += 4) {
337-
words.push((secureRandom() * 0x100000000) | 0);
361+
words.push(cryptoSecureRandomInt());
338362
}
339363

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

crypto-js.js

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,58 @@
1313
}
1414
}(this, function () {
1515

16+
/*globals window, global, require*/
17+
1618
/**
1719
* CryptoJS core components.
1820
*/
1921
var CryptoJS = CryptoJS || (function (Math, undefined) {
2022

23+
var crypto;
24+
25+
// Native crypto from window (Browser)
26+
if (typeof window !== 'undefined' && window.crypto) {
27+
crypto = window.crypto;
28+
}
29+
30+
// Native (experimental IE 11) crypto from window (Browser)
31+
if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
32+
crypto = window.msCrypto;
33+
}
34+
35+
// Native crypto from global (NodeJS)
36+
if (!crypto && typeof global !== 'undefined' && global.crypto) {
37+
crypto = global.crypto;
38+
}
39+
40+
// Native crypto import via require (NodeJS)
41+
if (!crypto && typeof require === 'function') {
42+
try {
43+
crypto = require('crypto');
44+
} catch (err) {}
45+
}
46+
2147
/*
2248
* Cryptographically secure pseudorandom number generator
2349
*
2450
* As Math.random() is cryptographically not safe to use
2551
*/
26-
var secureRandom = function () {
27-
// Native crypto module on NodeJS environment
28-
try {
29-
// Crypto from global object
30-
var crypto = global.crypto;
31-
32-
// Create a random float number between 0 and 1
33-
return Number('0.' + crypto.randomBytes(3).readUIntBE(0, 3));
34-
} catch (err) {}
35-
36-
// Native crypto module in Browser environment
37-
try {
38-
// Support experimental crypto module in IE 11
39-
var crypto = window.crypto || window.msCrypto;
52+
var cryptoSecureRandomInt = function () {
53+
if (crypto) {
54+
// Use getRandomValues method (Browser)
55+
if (typeof crypto.getRandomValues === 'function') {
56+
try {
57+
return crypto.getRandomValues(new Uint32Array(1))[0];
58+
} catch (err) {}
59+
}
4060

41-
// Create a random float number between 0 and 1
42-
return Number('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]);
43-
} catch (err) {}
61+
// Use randomBytes method (NodeJS)
62+
if (typeof crypto.randomBytes === 'function') {
63+
try {
64+
return crypto.randomBytes(4).readInt32LE();
65+
} catch (err) {}
66+
}
67+
}
4468

4569
throw new Error('Native crypto module could not be used to get secure random number.');
4670
};
@@ -334,7 +358,7 @@
334358
var words = [];
335359

336360
for (var i = 0; i < nBytes; i += 4) {
337-
words.push((secureRandom() * 0x100000000) | 0);
361+
words.push(cryptoSecureRandomInt());
338362
}
339363

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crypto-js",
3-
"version": "3.2.0",
3+
"version": "3.2.1",
44
"description": "JavaScript library of crypto standards.",
55
"license": "MIT",
66
"author": {

0 commit comments

Comments
 (0)