def __init__(self, amount, sender_public_key, recipient_public_key, sender_private_key): self.amount = amount self.sender = sender_public_key self.recipient = recipient_public_key self.sign, _ = sign_message( str(amount) + key_to_string(sender_public_key) + key_to_string(recipient_public_key), sender_private_key)
from encode import encode,decode import signature # This is the test data to sign data = 'test signed data' # Generate a PyCrypto RSA key using signature module key = signature.generate_key() # Sign the test data with the key sig = signature.sign(key, data) # Dump encoded versions of the keys and data to the console print 'private', signature.key_to_string(key) print 'public', signature.key_to_string(key.publickey()) print 'address', signature.public_key_to_address(key.publickey()) print 'data', data print 'signature', sig, '\n' # Test verification code print 'call to verify() with legit data\t\t\t%s\t(Should be True)' % signature.verify(key, sig, data) print 'call of verify() with tampered data\t\t\t%s\t(Should be False)' % key.verify(data+'asdf',(decode(encode(sig),type='long'),None))
def privkey(self): return key_to_string(self.private_key)
def pubkey(self): return key_to_string(self.public_key)
def recipient_str(self): return key_to_string(self.recipient)
def sender_str(self): return key_to_string(self.sender)
def to_string(self): return str(self.amount) + key_to_string(self.sender) + key_to_string( self.recipient) + str(self.sign)
def valid(self): plaintext = str(self.amount) + key_to_string( self.sender) + key_to_string(self.recipient) Hash = SHA256.new(plaintext.encode()).digest() return verify_message(self.sign, Hash, self.sender)