diff --git a/CanvasBlocker@kkapsner.de-0.3.1-Development.xpi b/canvasblocker.xpi similarity index 61% rename from CanvasBlocker@kkapsner.de-0.3.1-Development.xpi rename to canvasblocker.xpi index 561b71b..126177a 100644 Binary files a/CanvasBlocker@kkapsner.de-0.3.1-Development.xpi and b/canvasblocker.xpi differ diff --git a/lib/randomSupplies.js b/lib/randomSupplies.js index 4b9c062..83552b4 100644 --- a/lib/randomSupplies.js +++ b/lib/randomSupplies.js @@ -5,22 +5,30 @@ (function(){ "use strict"; - const persistentRnd = {}; + const persistentRnd = Object.create(null); exports.persistent = { getRng: function(length, window){ var domain = window.document.location.host; if (!persistentRnd[domain]){ + // create the (sub-)domains random numbers if not existing persistentRnd[domain] = new Uint8Array(128); - for (var i = 0; i < persistentRnd[domain].length; i += 1){ - persistentRnd[domain][i] = Math.floor(Math.random() * 0xFF); - } + window.crypto.getRandomValues(persistentRnd[domain]); } var bitSet = persistentRnd[domain]; return function(value, i){ + // use the last 7 bits from the value for the index of the + // random number var index = value & 0x7F; + + // use the last 3 bits from the position and the first bit from + // from the value to get bit to use from the random number var bitIndex = ((i & 0x03) << 1) | (value >>> 7); + + // extract the bit var bit = (bitSet[index] >>> bitIndex) & 0x01; + + // XOR the bit the the value to alter the last bit of it... or not return value ^ bit; }; } @@ -28,20 +36,24 @@ exports.nonPersistent = { getRng: function(length, window){ + // Initialize the random number batch creation var randomI = 65536; - // var randomOffset = 0; var randomNumbers = new Uint8Array(Math.min(65536, length)); return function(value, i){ if (randomI >= randomNumbers.length){ + // refill the random number bucket if empty randomI = 0; - // randomOffset += randomNumbers.length; if (length - i < 65536){ randomNumbers = new Uint8Array(length - i); } window.crypto.getRandomValues(randomNumbers); } var rnd = randomNumbers[randomI]; + randomI += 1; + + // do not alter the most significant bit that is set and + // the bit after it, to not disturb the image too much. if (value >= 0x80){ value = value ^ (rnd & 0x1F); } @@ -60,7 +72,6 @@ // else if (value >= 0x04){ // value = value ^ (rnd * 0x00); // } - randomI += 1; return value; }; }