2019-12-16 19:27:28 +01:00
|
|
|
// eslint-disable-next-line no-var
|
2019-05-24 18:31:11 +02:00
|
|
|
var log = function(){
|
|
|
|
"use strict";
|
|
|
|
return function log(...str){
|
2019-11-28 01:26:35 +01:00
|
|
|
if (str[str.length - 1] === "match"){
|
|
|
|
str.unshift("color: green");
|
|
|
|
str.unshift("%cOK");
|
|
|
|
}
|
|
|
|
else if (str[str.length - 1].substr(0, 9) === "missmatch"){
|
|
|
|
str.unshift("color: red");
|
|
|
|
str.unshift("%cX");
|
|
|
|
}
|
2019-05-24 18:31:11 +02:00
|
|
|
console.log(...str);
|
|
|
|
};
|
|
|
|
}();
|
|
|
|
function draw(canvas){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
canvas.setAttribute("width", 220);
|
|
|
|
canvas.setAttribute("height", 30);
|
|
|
|
|
2019-12-16 19:27:28 +01:00
|
|
|
const fp_text = "BrowserLeaks,com <canvas> 10";
|
2019-05-24 18:31:11 +02:00
|
|
|
|
2019-12-16 19:27:28 +01:00
|
|
|
const ctx = canvas.getContext("2d");
|
2019-05-24 18:31:11 +02:00
|
|
|
ctx.textBaseline = "top";
|
|
|
|
ctx.font = "14px 'Arial'";
|
|
|
|
ctx.textBaseline = "alphabetic";
|
|
|
|
ctx.fillStyle = "#f60";
|
|
|
|
ctx.fillRect(125, 1, 62, 20);
|
|
|
|
ctx.fillStyle = "#069";
|
|
|
|
ctx.fillText(fp_text, 2, 15);
|
|
|
|
ctx.fillStyle = "rgba(102, 204, 0, 07)";
|
|
|
|
ctx.fillText(fp_text, 4, 17);
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
}
|
|
|
|
function test(window){
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// create window canvas
|
2019-12-16 19:27:28 +01:00
|
|
|
const canvas = document.createElement("canvas");
|
2019-05-24 18:31:11 +02:00
|
|
|
// draw image in window canvas
|
2019-11-28 01:26:35 +01:00
|
|
|
draw(canvas);
|
2019-05-24 18:31:11 +02:00
|
|
|
return window.HTMLCanvasElement.prototype.toDataURL.call(canvas);
|
|
|
|
}
|
|
|
|
|
2019-12-16 19:27:28 +01:00
|
|
|
async function hash(string){
|
2019-05-24 18:31:11 +02:00
|
|
|
"use strict";
|
|
|
|
|
2019-12-16 19:27:28 +01:00
|
|
|
const buffer = new TextEncoder("utf-8").encode(string);
|
|
|
|
const hash = await crypto.subtle.digest("SHA-256", buffer);
|
|
|
|
const chunks = [];
|
|
|
|
(new Uint32Array(hash)).forEach(function(num){
|
|
|
|
chunks.push(num.toString(16));
|
2019-05-24 18:31:11 +02:00
|
|
|
});
|
2019-12-16 19:27:28 +01:00
|
|
|
return chunks.map(function(chunk){
|
|
|
|
return "0".repeat(8 - chunk.length) + chunk;
|
|
|
|
}).join("");
|
2019-05-24 18:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function compare(string1, string2, alwaysOutputHashes){
|
|
|
|
"use strict";
|
2019-12-16 19:27:28 +01:00
|
|
|
async function outputHashes(message){
|
|
|
|
const hashes = await Promise.all([
|
2019-05-24 18:31:11 +02:00
|
|
|
hash(string1),
|
|
|
|
hash(string2)
|
2019-12-16 19:27:28 +01:00
|
|
|
]);
|
|
|
|
console.log(message, ...hashes);
|
2019-05-24 18:31:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (string1 === string2){
|
|
|
|
if (alwaysOutputHashes){
|
|
|
|
outputHashes("Matching hashes:");
|
|
|
|
}
|
|
|
|
return "match";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
outputHashes("Hashes that differ:");
|
|
|
|
if (string1.length === string2.length){
|
|
|
|
let i;
|
|
|
|
for (i = 0; i < string1.length; i += 1){
|
|
|
|
if (string1.charAt(i) !== string2.charAt(i)){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "missmatch (first at " + i + ")";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let i;
|
|
|
|
for (i = 0; i < Math.min(string1.length, string2.length); i += 1){
|
|
|
|
if (string1.charAt(i) !== string2.charAt(i)){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return "missmatch (different lengths, first at " + i + ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-16 19:27:28 +01:00
|
|
|
// eslint-disable-next-line no-var
|
2019-05-24 18:31:11 +02:00
|
|
|
var reference = test(window);
|
2019-12-16 19:27:28 +01:00
|
|
|
(async function(){
|
2019-11-28 01:26:35 +01:00
|
|
|
"use strict";
|
2019-12-16 19:27:28 +01:00
|
|
|
try {
|
|
|
|
const hashValue = await hash(reference);
|
|
|
|
log("reference hash:", hashValue);
|
|
|
|
}
|
|
|
|
catch (error){
|
|
|
|
log("%cX", "color: red", "Unable to compute reference hash:", error);
|
|
|
|
}
|
|
|
|
}());
|