ecdsa.com
Python cryptographyPython cryptography

Error message

cryptography.exceptions.InvalidSignature

The exception is raised with no message text — the traceback ends in the bare class name (reproduced on cryptography 50.0).

Python cryptography — raised by verify()

What it means

public_key.verify(signature, data, ...) determined that the signature does not verify for this data under this key. Everything parsed; the check simply failed. Because the exception carries no detail by design, diagnosis means testing each input — key, data bytes, signature encoding, hash — separately.

Why it happens

How to fix it

  1. 1.

    Convert raw signatures to DER

    If the signature is 64 bytes (P-256), split it into r and s and re-encode as DER before verifying.

    python
    from cryptography.hazmat.primitives.asymmetric.utils import encode_dss_signature
    
    r = int.from_bytes(raw_sig[:32], "big")
    s = int.from_bytes(raw_sig[32:], "big")
    der_sig = encode_dss_signature(r, s)
    public_key.verify(der_sig, message, ec.ECDSA(hashes.SHA256()))
  2. 2.

    Use Prehashed for pre-computed digests

    When you hold the digest instead of the message, say so explicitly — otherwise the library hashes your hash.

    python
    from cryptography.hazmat.primitives.asymmetric import utils
    
    public_key.verify(
        der_sig,
        digest_bytes,                                    # already SHA-256(message)
        ec.ECDSA(utils.Prehashed(hashes.SHA256())),
    )
  3. 3.

    Pin down which input is wrong

    Paste key, message and signature into the error explainer below: it tests the usual mismatch hypotheses — wrong hash, raw vs DER, double hashing, high-S normalization, wrong curve — and names the one that makes your signature verify.

Related errors

← Browse the full signature error database