Skip to content

Instantly share code, notes, and snippets.

@nakov
Created April 5, 2018 10:31
Show Gist options
  • Save nakov/b0dccf6cea1a5fe7ca52cf6f4f0c5eaa to your computer and use it in GitHub Desktop.
Save nakov/b0dccf6cea1a5fe7ca52cf6f4f0c5eaa to your computer and use it in GitHub Desktop.
ECDSA with sec256k1 Example in Python - Generate Keys, Sign, Verify
import eth_keys, eth_utils, binascii, os
# privKey = eth_keys.keys.PrivateKey(os.urandom(32))
privKey = eth_keys.keys.PrivateKey(binascii.unhexlify(
'97ddae0f3a25b92268175400149d65d6887b9cefaf28ea2c078e05cdc15a3c0a'))
pubKey = privKey.public_key
pubKeyCompressed = '0' + str(2 + int(pubKey) % 2) + str(pubKey)[2:66]
address = pubKey.to_checksum_address()
print('Private key (64 hex digits):', privKey)
print('Public key (plain, 128 hex digits):', pubKey)
print('Public key (compressed, 66 hex digits):', pubKeyCompressed)
print('Signer address:', address)
print()
msg = b'Message for signing'
signature = privKey.sign_msg(msg)
print('Msg:', msg)
print('Msg hash:', binascii.hexlify(eth_utils.keccak(msg)))
print('Signature: [v = {0}, r = {1}, s = {2}]'.format(
hex(signature.v), hex(signature.r), hex(signature.s)))
print('Signature (130 hex digits):', signature)
print()
msg = b'Message for signing'
msgSigner = '0xa44f70834a711F0DF388ab016465f2eEb255dEd0'
signature = eth_keys.keys.Signature(binascii.unhexlify(
'6f0156091cbe912f2d5d1215cc3cd81c0963c8839b93af60e0921b61a19c54300c71006dd93f3508c432daca21db0095f4b16542782b7986f48a5d0ae3c583d401'))
signerRecoveredPubKey = signature.recover_public_key_from_msg(msg)
signerRecoveredAddress = signerRecoveredPubKey.to_checksum_address()
print('Signer public key (128 hex digits):', signerRecoveredPubKey)
print('Signer address:', signerRecoveredAddress)
print('Signature valid?:', signerRecoveredAddress == msgSigner)
@BitByteMaster
Copy link

I ran it myself and found the signature not valid hence the code returned False...

@adlingepa
Copy link

Can we do the same above operations (Generate Keys, Sign, Verify) for ed25519 using openssl command ?
If yes, can you please guide me with commands or some references ?

@nakov
Copy link
Author

nakov commented Nov 28, 2020

It should work like this:

openssl genpkey -algorithm Ed25519 -out ed25519key-private.pem
openssl dgst -sha256 -sign ed25519key-private.pem -out signature-sha256.txt filetosign.txt
openssl dgst -sha256 -verify ed25519key-public.pem -signature signature-sha256.txt filetosign.txt

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment