webcamContentCheck: use browser's SHA-1 hash from Web Crypto API instead

of a custom hash function
This commit is contained in:
Brent Baccala 2022-07-26 15:51:56 -04:00
parent 776fd9f024
commit 279c3e71e2

View File

@ -13,39 +13,18 @@ async function webcamContentCheck(test) {
let check;
for (let i = repeats; i >= 1; i--) {
console.log(`loop ${i}`);
const checkCameras = () => {
const checkCameras = async () => {
const videos = document.querySelectorAll('video');
const lastVideoHash = document.lastVideoHash || {};
document.lastVideoHash = lastVideoHash;
// If this code was running in the playwright test harness, we
// could just require('sha1'), but it's running in the test
// browser via test.page.evaluate, and I'm not sure how to get a
// node module (like sha1) into the test browser. So I just
// found a nice hash function on the Internet. -bwb
// public domain code from https://stackoverflow.com/a/52171480/1493790
// and/or https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
const cyrb53 = function(array, seed = 0) {
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
for (let i = 0, ch; i < array.length; i++) {
ch = array[i];
h1 = Math.imul(h1 ^ ch, 2654435761);
h2 = Math.imul(h2 ^ ch, 1597334677);
}
h1 = Math.imul(h1 ^ (h1>>>16), 2246822507) ^ Math.imul(h2 ^ (h2>>>13), 3266489909);
h2 = Math.imul(h2 ^ (h2>>>16), 2246822507) ^ Math.imul(h1 ^ (h1>>>13), 3266489909);
return 4294967296 * (2097151 & h2) + (h1>>>0);
};
for (let v = 0; v < videos.length; v++) {
const video = videos[v];
const canvas = document.createElement('canvas');
const context = canvas.getContext('2d');
context.drawImage(video, 0, 0, video.videoWidth, video.videoHeight);
const pixel = context.getImageData(0, 0, video.videoWidth, video.videoHeight).data;
const pixelHash = cyrb53(pixel);
const pixelHash = await window.crypto.subtle.digest('SHA-1', pixel);
if (lastVideoHash[v]) {
if (lastVideoHash[v] == pixelHash) {
@ -57,7 +36,7 @@ async function webcamContentCheck(test) {
return true;
};
check = await test.page.evaluate(checkCameras, i);
check = await test.page.evaluate(checkCameras);
if (!check) return false;
await sleep(LOOP_INTERVAL);
}