Skip to content

Commit 793d65c

Browse files
committed
Merge branch 'feature/secure-random' into develop
2 parents 8e6d15b + eb61233 commit 793d65c

File tree

2 files changed

+84
-76
lines changed

2 files changed

+84
-76
lines changed

grunt/tasks/modularize.js

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
var _ = require("lodash"),
44

5-
fmd = require("fmd");
5+
fmd = require("fmd");
66

77
module.exports = function (grunt) {
88

@@ -14,78 +14,76 @@ module.exports = function (grunt) {
1414
modules = {},
1515

1616
config = {
17-
target: this.target + '/',
18-
factories: ["commonjs", "amd", "global"],
19-
trim_whitespace: true,
20-
new_line: "unix",
21-
indent: "\t"
22-
};
17+
target: this.target + '/',
18+
factories: ["commonjs", "amd", "global"],
19+
trim_whitespace: true,
20+
new_line: "unix",
21+
indent: "\t"
22+
};
2323

2424
// Prepare Factory-Module-Definition settings
25-
_.each(options, function (conf, name) {
25+
_.each(options, (conf, name) => {
2626
var sources = [],
2727

28-
opts = {
29-
depends: {}
30-
},
28+
opts = {
29+
depends: {}
30+
},
3131

32-
deps = [];
32+
deps = [];
3333

34-
if (conf.exports) {
35-
opts.exports = conf.exports;
36-
}
34+
if (conf.exports) {
35+
opts.exports = conf.exports;
36+
}
3737

38-
if (conf.global) {
39-
opts.global = conf.global;
40-
}
38+
if (conf.global) {
39+
opts.global = conf.global;
40+
}
4141

4242
// Find and add self as source
43-
_.each(this.filesSrc, function (source) {
44-
if (grunt.file.exists(source + name + ".js")) {
45-
sources.push(source + name + ".js");
46-
}
47-
}, this);
43+
_.each(this.filesSrc, (source) => {
44+
if (grunt.file.exists(source + name + ".js")) {
45+
sources.push(source + name + ".js");
46+
}
47+
});
4848

4949
if (conf.pack) {
50-
// Collect all components
51-
deps = _.chain(conf.components)
52-
.map(function (depName) {
53-
return options[depName].components;
54-
})
55-
.flatten()
56-
.uniq()
57-
.without(name)
58-
.sort(function (a, b) {
59-
return options[a].components.indexOf(b) === -1 ? -1 : 1;
60-
})
61-
.value();
50+
// Collect all components
51+
deps = _.chain(conf.components)
52+
.map(depName => options[depName].components)
53+
.flatten()
54+
.uniq()
55+
.without(name)
56+
.sort((a, b) => {
57+
return options[a].components.indexOf(b) === -1 ? -1 : 1;
58+
})
59+
.value();
6260

6361
// Add components as source files -> results a single file
64-
_.each(this.filesSrc, function (source) {
65-
_.each(deps, function (depName) {
66-
if (grunt.file.exists(source + depName + ".js")) {
67-
sources.push(source + depName + ".js");
68-
}
69-
});
70-
}, this);
62+
_.each(this.filesSrc, (source) => {
63+
_.each(deps, (depName) => {
64+
if (grunt.file.exists(source + depName + ".js")) {
65+
sources.push(source + depName + ".js");
66+
}
67+
});
68+
});
7169
} else {
72-
// Read components and add them as dependecies
73-
_.each(_.without(conf.components, name), function (value, i) {
74-
opts.depends['./' + value] = value === "core" ? "CryptoJS" : null;
75-
});
76-
}
70+
// Read components and add them as dependecies
71+
_.each(_.without(conf.components, name), (value, i) => {
72+
opts.depends['./' + value] = value === "core" ? "CryptoJS" : null;
73+
});
74+
}
7775

78-
// Remove duplicates
79-
sources = _.uniq(sources);
76+
// Remove duplicates
77+
sources = _.uniq(sources);
8078

8179
// Add module settings to fmd definition
82-
modules[name] = [sources, opts];
83-
}, this);
80+
modules[name] = [sources, opts];
81+
});
8482

85-
// Build packege modules
86-
fmd(config)
87-
.define(modules)
88-
.build(function (createdFiles) {
83+
// Build packege modules
84+
fmd(config)
85+
.define(modules)
86+
.build(() => {
8987

9088
done();
9189
});

src/core.js

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,34 @@
22
* CryptoJS core components.
33
*/
44
var CryptoJS = CryptoJS || (function (Math, undefined) {
5+
6+
/*
7+
* Cryptographically secure pseudorandom number generator
8+
*
9+
* As Math.random() is cryptographically not safe to use
10+
*/
11+
var secureRandom = function () {
12+
// Native crypto module on NodeJS environment
13+
try {
14+
// Crypto from global object
15+
var crypto = global.crypto;
16+
17+
// Create a random float number between 0 and 1
18+
return Number('0.' + crypto.randomBytes(3).readUIntBE(0, 3));
19+
} catch (err) {}
20+
21+
// Native crypto module in Browser environment
22+
try {
23+
// Support experimental crypto module in IE 11
24+
var crypto = window.crypto || window.msCrypto;
25+
26+
// Create a random float number between 0 and 1
27+
return Number('0.' + window.crypto.getRandomValues(new Uint32Array(1))[0]);
28+
} catch (err) {}
29+
30+
throw new Error('Native crypto module could not be used to get secure random number.');
31+
};
32+
533
/*
634
* Local polyfil of Object.create
735
*/
@@ -289,26 +317,8 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
289317
random: function (nBytes) {
290318
var words = [];
291319

292-
var r = function (m_w) {
293-
var m_w = m_w;
294-
var m_z = 0x3ade68b1;
295-
var mask = 0xffffffff;
296-
297-
return function () {
298-
m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
299-
m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
300-
var result = ((m_z << 0x10) + m_w) & mask;
301-
result /= 0x100000000;
302-
result += 0.5;
303-
return result * (Math.random() > 0.5 ? 1 : -1);
304-
}
305-
};
306-
307-
for (var i = 0, rcache; i < nBytes; i += 4) {
308-
var _r = r((rcache || Math.random()) * 0x100000000);
309-
310-
rcache = _r() * 0x3ade67b7;
311-
words.push((_r() * 0x100000000) | 0);
320+
for (var i = 0; i < nBytes; i += 4) {
321+
words.push((secureRandom() * 0x100000000) | 0);
312322
}
313323

314324
return new WordArray.init(words, nBytes);
@@ -540,7 +550,7 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
540550
*/
541551
_process: function (doFlush) {
542552
var processedWords;
543-
553+
544554
// Shortcuts
545555
var data = this._data;
546556
var dataWords = data.words;

0 commit comments

Comments
 (0)