ecdsa is a pure Python implementation of Elliptic Curve Cryptography (ECC) supporting ECDSA (Elliptic Curve Digital Signature Algorithm), EdDSA (Edwards-curve Digital Signature Algorithm), and ECDH (Elliptic Curve Diffie-Hellman). It is actively maintained with several releases per year, providing a robust solution for digital signatures in Python applications. The current version is 0.19.2.
pip install ecdsaVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to generate an ECDSA private and public key pair using the NIST256p curve, sign a message digest with the private key, and then verify the signature using the corresponding public key.
For security-sensitive applications where side-channel resistance is critical, consider using a cryptographic library with constant-time implementations (e.g., `cryptography` library) or ensure your environment mitigates timing attacks.
Update to version 0.19.2 or later to ensure robust DER parsing and prevent potential DoS from malformed inputs. Carefully validate any DER-encoded inputs from untrusted sources.
Always specify `format="pkcs8"` when calling `to_pem()` or `to_der()` for private keys: `key.to_pem(format="pkcs8")`.
Upgrade to a currently supported Python version (e.g., Python 3.8 or newer).
Handle the return value as `bytes` when using `to_string()`.
Ensure you store and provide the correct elliptic curve (e.g., `NIST256p`) when using `from_string()`.
Install the library using pip: `pip install ecdsa`.
Correct the curve name to 'SECP256k1': `from ecdsa import SECP256k1, SigningKey`.
When verifying, ensure you explicitly provide the `hashfunc` argument, or use the `VerifyingKey.from_string` (or similar) method to properly reconstruct the key if it was serialized, and ensure compatibility with the library version. Example: `verifying_key.verify(signature, message_hash, sigdecode=ecdsa.util.sigdecode_string, hashfunc=hashlib.sha256)`.
Refer to the `ecdsa.curves` module for available curves (e.g., `ecdsa.NIST224p` or `ecdsa.SECP256k1`) or use `ecdsa.curves.Curve` to define a custom curve if it's supported and parameters are known.
Ensure the message is hashed correctly before signing and verifying, the correct public key is used, and the signature encoding (e.g., DER) is consistently applied during both signing and verification. Double-check the elliptic curve used for key generation and signature operations.