识别cavas指纹是否被篡改

2026-02-10

Written by: Jack-S-H

基于python的pywinauto库

Security

Js

工作量证明方法——要求用户绘制画布并验证某些已知像素的数值.

  生成一个特定颜色的画布,并验证特定像素的数值是否符合预期的。下面的代码片段展示了一个简单的示例,我们将画布填充为 rgba(0, 127, 255, 1)。然后,我们对每个像素进行迭代,验证 r、g、b、a 的分量是否具有期望值(因此代码中使用%4)。

示例代码:

var canvas = document.createElement("canvas");
canvas.height = size;
canvas.width = size;
var context = canvas.getContext("2d");
context.fillStyle = "rgba(0, 127, 255, 1)";
var pixelValues = [0, 127, 255, 255];
// We apply the color rgba(0, 127, 255, 1) to the whole canvas
context.fillRect(0, 0, canvas.width, canvas.height);
var pixels = context.getImageData(0, 0, canvas.width, canvas.height).data;
for (var i = 0; i < pixels.length; i += 1) {
    if (pixels[i] !== pixelValues[i % 4]) {
        // we test if the pixel has the expected value
        // If that's not the case it means the canvas value has been modified
        console.log('Canvas has been overridden!')
    }
}