Skip to content

Commit

Permalink
Implement a secure random as suggested by @KenanSulayman in issue brix#7
Browse files Browse the repository at this point in the history
  • Loading branch information
evanvosberg committed Jun 19, 2014
1 parent 794d5d5 commit ff1f003
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions src/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,27 @@ var CryptoJS = CryptoJS || (function (Math, undefined) {
*/
random: function (nBytes) {
var words = [];
for (var i = 0; i < nBytes; i += 4) {
words.push((Math.random() * 0x100000000) | 0);

var r = (function (m_w) {
var m_w = m_w;
var m_z = 0x3ade68b1;
var mask = 0xffffffff;

return function () {
m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
var result = ((m_z << 0x10) + m_w) & mask;
result /= 0x100000000;
result += 0.5;
return result * (Math.random() > .5 ? 1 : -1);
}
});

for (var i = 0, rcache; i < nBytes; i += 4) {
var _r = r((rcache || Math.random()) * 0x100000000);

rcache = _r() * 0x3ade67b7;
words.push((_r() * 0x100000000) | 0);
}

return new WordArray.init(words, nBytes);
Expand Down

0 comments on commit ff1f003

Please sign in to comment.