1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-22 21:00:23 +01:00
CanvasBlocker/lib/randomSupplies.js

86 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-08-06 19:17:36 +02:00
/* jslint moz: true, bitwise: true */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
function getDomain(window){
if (!window.location.href || window.location.href === "about:blank"){
if (window !== window.parent){
return getDomain(window.parent);
}
else if (window.opener){
return getDomain(window.opener);
}
}
return window.location.host;
}
const getPersistentRnd = (function(){
var persistentRnd = Object.create(null);
const {process} = require("sdk/remote/child");
process.port.on("canvasBlocker-set-domain-rnd", function(process, {domain, rnd}){
persistentRnd[domain] = new Uint8Array(rnd);
});
process.port.on("canvasBlocker-clear-domain-rnd", function(){
persistentRnd = Object.create(null);
});
return function getPersistentRnd(window){
var domain = getDomain(window);
2016-08-06 19:17:36 +02:00
if (!persistentRnd[domain]){
// create the (sub-)domains random numbers if not existing
2016-08-06 19:17:36 +02:00
persistentRnd[domain] = new Uint8Array(128);
window.crypto.getRandomValues(persistentRnd[domain]);
process.port.emit("canvasBlocker-new-domain-rnd", {domain, rnd: Array.from(persistentRnd[domain])});
2016-08-06 19:17:36 +02:00
}
return persistentRnd[domain];
}
}());
exports.persistent = {
getRng: function(length, window){
var bitSet = getPersistentRnd(window);
2016-08-06 19:17:36 +02:00
return function(value, i){
// use the last 7 bits from the value for the index of the
// random number
2016-08-06 19:17:36 +02:00
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
2016-08-06 19:17:36 +02:00
var bitIndex = ((i & 0x03) << 1) | (value >>> 7);
// extract the bit
2016-08-06 19:17:36 +02:00
var bit = (bitSet[index] >>> bitIndex) & 0x01;
// XOR the bit and the value to alter the last bit of it... or not
2016-08-06 19:17:36 +02:00
return value ^ bit;
};
}
};
exports.nonPersistent = {
getRng: function(length, window){
// Initialize the random number batch creation
2016-08-06 19:17:36 +02:00
var randomI = 65536;
var randomNumbers = new Uint8Array(Math.min(65536, length));
return function(value, i){
if (randomI >= randomNumbers.length){
// refill the random number bucket if empty
2016-08-06 19:17:36 +02:00
randomI = 0;
if (length - i < 65536){
randomNumbers = new Uint8Array(length - i);
}
window.crypto.getRandomValues(randomNumbers);
}
var rnd = randomNumbers[randomI];
randomI += 1;
// XOR the last bit to alter it... or not
return value ^ (rnd & 0x01);
2016-08-06 19:17:36 +02:00
};
}
};
}());