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";
|
|
|
|
|
2016-08-15 13:27:28 +02:00
|
|
|
const persistentRnd = Object.create(null);
|
2016-08-06 19:17:36 +02:00
|
|
|
exports.persistent = {
|
|
|
|
getRng: function(length, window){
|
|
|
|
var domain = window.document.location.host;
|
|
|
|
if (!persistentRnd[domain]){
|
2016-08-15 13:27:28 +02:00
|
|
|
// create the (sub-)domains random numbers if not existing
|
2016-08-06 19:17:36 +02:00
|
|
|
persistentRnd[domain] = new Uint8Array(128);
|
2016-08-15 13:27:28 +02:00
|
|
|
window.crypto.getRandomValues(persistentRnd[domain]);
|
2016-08-06 19:17:36 +02:00
|
|
|
}
|
|
|
|
var bitSet = persistentRnd[domain];
|
|
|
|
|
|
|
|
return function(value, i){
|
2016-08-15 13:27:28 +02:00
|
|
|
// 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;
|
2016-08-15 13:27:28 +02:00
|
|
|
|
|
|
|
// 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);
|
2016-08-15 13:27:28 +02:00
|
|
|
|
|
|
|
// extract the bit
|
2016-08-06 19:17:36 +02:00
|
|
|
var bit = (bitSet[index] >>> bitIndex) & 0x01;
|
2016-08-15 13:27:28 +02:00
|
|
|
|
|
|
|
// XOR the bit the 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){
|
2016-08-15 13:27:28 +02:00
|
|
|
// 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){
|
2016-08-15 13:27:28 +02:00
|
|
|
// 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];
|
2016-08-15 13:27:28 +02:00
|
|
|
randomI += 1;
|
|
|
|
|
|
|
|
// do not alter the most significant bit that is set and
|
|
|
|
// the bit after it, to not disturb the image too much.
|
2016-08-06 19:17:36 +02:00
|
|
|
if (value >= 0x80){
|
|
|
|
value = value ^ (rnd & 0x1F);
|
|
|
|
}
|
|
|
|
else if (value >= 0x40){
|
|
|
|
value = value ^ (rnd & 0x0F);
|
|
|
|
}
|
|
|
|
else if (value >= 0x20){
|
|
|
|
value = value ^ (rnd & 0x07);
|
|
|
|
}
|
|
|
|
else if (value >= 0x10){
|
|
|
|
value = value ^ (rnd & 0x03);
|
|
|
|
}
|
|
|
|
else if (value >= 0x08){
|
|
|
|
value = value ^ (rnd & 0x01);
|
|
|
|
}
|
|
|
|
// else if (value >= 0x04){
|
|
|
|
// value = value ^ (rnd * 0x00);
|
|
|
|
// }
|
|
|
|
return value;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}());
|