Fixed fallback randomBytes for weak-crypto browsers..

This commit is contained in:
ricmoo 2017-03-08 01:51:04 -05:00
parent 33a1459505
commit 730ccd36b2
1 changed files with 9 additions and 4 deletions

View File

@ -6,10 +6,15 @@ var crypto = global.crypto || global.msCrypto;
if (!crypto || !crypto.getRandomValues) {
console.log('WARNING: Missing strong random number source; using weak randomBytes');
crypto = {
getRandomValues: function(length) {
for (var i = 0; i < buffer.length; i++) {
buffer[i] = parseInt(256 * Math.random());
getRandomValues: function(buffer) {
for (var round = 0; round < 20; round++) {
for (var i = 0; i < buffer.length; i++) {
if (round) {
buffer[i] ^= parseInt(256 * Math.random());
} else {
buffer[i] = parseInt(256 * Math.random());
}
}
}
return buffer;