Add sign/verify functions

pull/43/head
tuxxy 2017-09-18 10:25:36 -07:00
parent bf08aa5b53
commit 76485f7b06
1 changed files with 37 additions and 1 deletions

View File

@ -1,5 +1,6 @@
import msgpack
from random import SystemRandom
from py_ecc.secp256k1 import N, privtopub
from py_ecc.secp256k1 import N, privtopub, ecdsa_raw_sign, ecdsa_raw_recover
class SigningKeypair(object):
@ -13,3 +14,38 @@ class SigningKeypair(object):
self.priv_key = priv_number.to_bytes(32, byteorder='big')
# Get the public component
self.pub_key = privtopub(self.priv_key)
def sign(self, msghash):
"""
Signs a hashed message and returns a msgpack'ed v, r, and s.
:param bytes msghash: Hash of the message
:rtype: Bytestring
:return: Msgpacked bytestring of v, r, and s (the signature)
"""
v, r, s = ecdsa_raw_sign(msghash, self.priv_key)
v_bytes = v.to_bytes(1, byteorder='big')
r_bytes = r.to_bytes(32, byteorder='big')
s_bytes = s.to_bytes(32, byteorder='big')
return msgpack.dumps((v_bytes + r_bytes + s_bytes))
def verify(self, msghash, signature):
"""
Takes a msgpacked signature and verifies the message.
:param bytes msghash: The hashed message to verify
:param bytes signature: The msgpacked signature (v, r, and s)
:rtype: Boolean
:return: Is the signature valid or not?
"""
sig = msgpack.loads(signature)
v = int.from_bytes(sig[0], byteorder='big')
r = int.from_bytes(sig[1], byteorder='big')
s = int.from_bytes(sig[2], byteorder='big')
# Generate the public key from the signature and validate
# TODO: Look into fixed processing time functions for comparison
verify_sig = ecdsa_raw_recover(msghash, (v, r, s))
return verify_sig == self.pub_key