The whole verification is eight lines
This is the function, unedited, from analyzer/dkim.py. No imports
beyond the standard library; the RSA operation is Python’s built-in
pow() with three arguments, and the padding check is a byte
comparison against a block we rebuild ourselves.
def _rsa_verify(sig: bytes, digest: bytes, n: int, e: int, hashname: str) -> bool:
if int.from_bytes(sig, "big") >= n:
return False
k = (n.bit_length() + 7) // 8
em = pow(int.from_bytes(sig, "big"), e, n).to_bytes(k, "big")
t = _DIGEST_PREFIX[hashname] + digest
expected = b"\x00\x01" + b"\xff" * (k - len(t) - 3) + b"\x00" + t
return em == expected
It sits inside DAEMON, an email threat-intelligence console: 28 modules, 130 passing tests,
standard library at the core. The verifier was validated non-circularly: signed by a
third-party tool, verified by this code, then re-checked against a deliberately tampered copy.