From 699698f5297ecc936d431e3d11a797afc87cee78 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Sat, 14 May 2016 19:12:45 +0200 Subject: [PATCH] Improved faking performance. --- lib/modifiedAPI.js | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/lib/modifiedAPI.js b/lib/modifiedAPI.js index 944fad5..d130147 100644 --- a/lib/modifiedAPI.js +++ b/lib/modifiedAPI.js @@ -28,27 +28,40 @@ ); } data = imageData.data; + var l = data.length; + var randomI = 65536; + var randomOffset = 0; + var randomNumbers = new Uint8Array(Math.min(65536, l)); - for (var i = 0, l = data.length; i < l; i += 1){ + for (var i = 0; i < l; i += 1, randomI += 1){ + if (randomI >= randomNumbers.length){ + randomI = 0; + randomOffset += randomNumbers.length; + if (l - i < 65536){ + randomNumbers = new Uint8Array(l - i); + } + window.crypto.getRandomValues(randomNumbers); + } + var rnd = randomNumbers[randomI]; var value = source[i]; if (value >= 0x80){ - value = value ^ Math.floor(Math.random() * 0x20); + value = value ^ (rnd & 0x1F); } else if (value >= 0x40){ - value = value ^ Math.floor(Math.random() * 0x10); + value = value ^ (rnd & 0x0F); } else if (value >= 0x20){ - value = value ^ Math.floor(Math.random() * 0x08); + value = value ^ (rnd & 0x07); } else if (value >= 0x10){ - value = value ^ Math.floor(Math.random() * 0x04); + value = value ^ (rnd & 0x03); } else if (value >= 0x08){ - value = value ^ Math.floor(Math.random() * 0x02); - } - else if (value >= 0x04){ - value = value ^ Math.floor(Math.random() * 0x01); + value = value ^ (rnd & 0x01); } + // else if (value >= 0x04){ + // value = value ^ (rnd * 0x00); + // } data[i] = value; } var canvas = original.cloneNode(true);