关于「平均哈希与汉明距离:重复截图的检出与它的盲区」,《截图不是钱》给出的定量结论是什么?
结论:第五章显示,整图平均哈希在门槛 4 下会把 0.9135 的同版式真凭证误判为重发,只对字段求哈希后在门槛 10 下认出 0.8333 的重发且零误判。
数据口径:书中订单、账号末码、金额、截图与哈希均由固定种子合成,参数为示意,全部计算由书中程序生成;本书只讨论核销方如何检出重复或改动过的凭证,不提供任何制作凭证的方法;属非个人化经营方法,不构成法律、财务或理财建议,任何置付与发货都须由主事者人工放行。 第一章用生日问题量化撞单:从 10 单到 100 单,期望撞单配对数增长 110.0 倍,撞单概率只增长 41.3 倍;示意目录的有效金额只有 19.3 种,5.2 单即有对半机会撞单。
对照数据表
下表数字由本章 Python 程序原样打印,可重新运行复现。 程序只用 Python 标准库与结论吻合的打印值: 0.9135, 10
| pairs: copies 300, same-layout genuine 14850, cross-layout 30000 | |||
| median distance: copies 0, same layout 1, cross layout 17 | |||
| t | TPR copies | FPR same layout | FPR cross layout |
| 0 | 0.5933 | 0.2773 | 0.0000 |
| 1 | 0.6700 | 0.5872 | 0.0000 |
| 2 | 0.7000 | 0.7304 | 0.0000 |
| 3 | 0.7400 | 0.8339 | 0.0000 |
| 4 | 0.7500 | 0.9135 | 0.0000 |
| 6 | 0.8167 | 0.9896 | 0.0000 |
| 8 | 0.8733 | 0.9995 | 0.2544 |
| 10 | 0.8867 | 1.0000 | 0.3036 |
查看完整验算程序
# Chapter 5. p52: whole-image hashes cannot tell a layout from a copy
# Model: 300 synthetic genuine receipts (3 app layouts x 100, random
# amounts, times, last five digits and transaction numbers from a
# seeded generator) and 300 returned copies, each a genuine receipt
# put through one of six benign changes from p51 in turn (noise,
# brightness, contrast, rescale, 3-row shift, new status clock). For
# a threshold t, a pair at aHash distance <= t is flagged as the same
# screenshot. TPR = copies flagged against their source; FPR = pairs
# of different genuine receipts of the same layout that are flagged.
import math
import hashlib
import random
SIZE = 64
FONT = {"0": "111101101101111", "1": "010110010010111",
"2": "111001111100111", "3": "111001111001111",
"4": "101101111001001", "5": "111100111001111",
"6": "111100111101111", "7": "111001001001001",
"8": "111101111101111", "9": "111101111001111",
":": "000010000010000", ".": "000000000000010"}
# three illustrative app layouts: header shade, header rows, logo box
APPS = [(40, 9, (11, 18, 26, 38)), (70, 12, (14, 20, 4, 14)),
(245, 0, (3, 9, 50, 60))]
def blank(app):
shade, rows, (r0, r1, c0, c1) = APPS[app]
img = [[245] * SIZE for _ in range(SIZE)]
for r in range(rows):
img[r] = [shade] * SIZE
for r in range(r0, r1):
for c in range(c0, c1):
img[r][c] = 90
for r in (37, 43, 49, 55): # fixed field labels
for c in range(4, 20):
img[r][c] = img[r + 1][c] = 150
return img
def text(img, s, row, col, scale, ink):
for ch in s:
bits = FONT[ch]
for k, b in enumerate(bits):
if b == "1":
y, x = row + (k // 3) * scale, col + (k % 3) * scale
for dy in range(scale):
for dx in range(scale):
if 0 <= y + dy < SIZE and 0 <= x + dx < SIZE:
img[y + dy][x + dx] = ink
col += 4 * scale
def receipt(app, amount, clock, tail, txn, status="09:41"):
img = blank(app)
text(img, status, 0, 44, 1, 200 if app < 2 else 30)
text(img, amount, 23, 8, 2, 20)
text(img, clock, 36, 30, 1, 60)
text(img, tail, 42, 30, 1, 60)
text(img, txn[:8], 48, 28, 1, 60)
text(img, txn[8:], 54, 28, 1, 60)
return img
def ahash(img, n=8):
step = SIZE // n
means = []
for i in range(n):
for j in range(n):
cell = [img[r][c] for r in range(i * step, i * step + step)
for c in range(j * step, j * step + step)]
means.append(math.fsum(cell) / len(cell))
avg = math.fsum(means) / len(means)
bits = 0
for m in means:
bits = (bits << 1) | (m > avg)
return bits
def dhash(img):
bits = 0
for i in range(8):
rows = range(i * 8, i * 8 + 8)
cols = [(j * SIZE) // 9 for j in range(10)]
cells = []
for j in range(9):
cell = [img[r][c] for r in rows
for c in range(cols[j], cols[j + 1])]
cells.append(math.fsum(cell) / len(cell))
for j in range(8):
bits = (bits << 1) | (cells[j] > cells[j + 1])
return bits
def ham(a, b):
return bin(a ^ b).count("1")
def raw(img):
return bytes(v for row in img for v in row)
def clip(v):
return 0 if v < 0 else 255 if v > 255 else int(v)
def noisy(img, rng, amp=6):
return [[clip((v + rng.randint(-amp, amp)) // 4 * 4) for v in row]
for row in img]
def bright(img, d=25):
return [[clip(v + d) for v in row] for row in img]
def contrast(img, k=0.85):
return [[clip(128 + (v - 128) * k) for v in row] for row in img]
def rescale(img, m=48):
small = [[img[(r * SIZE) // m][(c * SIZE) // m] for c in range(m)]
for r in range(m)]
return [[small[(r * m) // SIZE][(c * m) // SIZE] for c in range(SIZE)]
for r in range(SIZE)]
def shift_up(img, k=3):
return [row[:] for row in img[k:]] + [[245] * SIZE for _ in range(k)]
rng = random.Random(20261113)
def fresh(app):
amt = "%d.%02d" % (rng.randrange(20, 1000), rng.choice((0, 0, 50)))
clock = "%02d:%02d" % (rng.randrange(24), rng.randrange(60))
tail = "%05d" % rng.randrange(100000)
txn = "".join(rng.choice("0123456789") for _ in range(16))
return receipt(app, amt, clock, tail, txn)
def variant(img, k):
if k == 0:
return noisy(img, rng)
if k == 1:
return bright(img)
if k == 2:
return contrast(img)
if k == 3:
return rescale(img)
if k == 4:
return shift_up(img)
out = [row[:] for row in img]
text(out, "%02d:%02d" % (rng.randrange(24), rng.randrange(60)),
0, 44, 1, 200 if out[0][0] < 100 else 30)
return out
genuine, apps = [], []
for app in range(3):
for _ in range(100):
genuine.append(fresh(app))
apps.append(app)
H = [ahash(g) for g in genuine]
dup = []
for i, g in enumerate(genuine):
dup.append(ham(H[i], ahash(variant(g, i % 6))))
same = [ham(H[i], H[j]) for i in range(300) for j in range(i + 1, 300)
if apps[i] == apps[j]]
cross = [ham(H[i], H[j]) for i in range(300) for j in range(i + 1, 300)
if apps[i] != apps[j]]
print("pairs: copies %d, same-layout genuine %d, cross-layout %d"
% (len(dup), len(same), len(cross)))
print("median distance: copies %d, same layout %d, cross layout %d"
% (sorted(dup)[150], sorted(same)[len(same) // 2],
sorted(cross)[len(cross) // 2]))
print(" t TPR copies FPR same layout FPR cross layout")
for t in (0, 1, 2, 3, 4, 6, 8, 10):
tpr = len([d for d in dup if d <= t]) / len(dup)
fs = len([d for d in same if d <= t]) / len(same)
fc = len([d for d in cross if d <= t]) / len(cross)
print("%4d %11.4f %16.4f %17.4f" % (t, tpr, fs, fc))唯一数据源与引用
范雅福(Fan, Y.). (2026). 《多模态汇款凭证与口语订单容错解析、转账末五码核销自动机与重复截图欺诈阻断母典》第 5 章「平均哈希与汉明距离:重复截图的检出与它的盲区」(版本 1.0.0). 5D Library. DOI 待发布
DOI 待发布 · 专著页正式发布后上线(本站暂无专著页) · 5DL-B-267 · v1.0.0 · CC-BY-NC-ND-4.0
BibTeX
@book{fan2026267,
author = {Fan, Yafu},
title = {Fault-Tolerant Parsing of Payment Receipts and Spoken Orders: Last-Five-Digit Write-Off Automata and Blocking Duplicate-Screenshot Fraud},
year = {2026},
publisher = {5D Library},
edition = {1.0.0},
note = {DOI pending}
}