简体   繁体   中英

How to verify long ECDSA signatures with python

I am trying to verify an ECDSA signature which is 71 bytes with Python using ecdsa package. Here is my code:

import ecdsa
from hashlib import sha256

sig = bytes.fromhex("3045022100C63ECC434A7D78DA9CCB8328BF87564FE1DF3F1F879E5C578DDF4637AE7C47790220266BC4491EA8FCF740DA00F14CCA1E67D9A7EADD48BC24D033499879A17D1BF7")
message = bytes.fromhex("0A8163888A52B2C873DD3730DED740B5FA4373438BC129E65CA8E9F955DA5FB3")
pubKey = '02b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737'
vk = ecdsa.VerifyingKey.from_string(bytes.fromhex(pubKey), curve=ecdsa.SECP256k1, hashfunc=sha256)
assert vk.verify(sig, message)

When I try to run this code, I get an error saying:

ecdsa.keys.BadSignatureError: ('Malformed formatting of signature', MalformedSignature('Invalid length of signature, expected 64 bytes long, provided string is 71 bytes long')) .

How can I verify this type of signature?

So there are two common encodings of ECDSA signatures. The standard one is 128 bytes. (Sorry, I said 64 in my comment above). The other is 70-72 bytes. The former is the "standard" encoding and is just two 64-byte strings concatenated. The latter is DER format.

The documentation for ECDSA says that you should be able to write:

import hashlib
from ecdsa.util import sigdecode_der
assert vk.verify(signature, data, hashlib.sha256, sigdecode=sigdecode_der)

no longer gives an error message about the wrong sized string, but it's still not verifying the signature.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM