Skip to content

Instantly share code, notes, and snippets.

@jaymine
Last active March 14, 2018 09:22
Show Gist options
  • Save jaymine/c1bc604f9d9bad9bc7daac30ffcabd84 to your computer and use it in GitHub Desktop.
Save jaymine/c1bc604f9d9bad9bc7daac30ffcabd84 to your computer and use it in GitHub Desktop.
from Crypto.Hash import SHA256
from Crypto.Signature import PKCS1_v1_5
from Crypto.PublicKey import RSA
def sign (digest, privatekeyfile):
private_key = False
with open(privatekeyfile, "r") as private_key_file: # Read private key from file
private_key = RSA.importKey(private_key_file.read())
signer = PKCS1_v1_5.new(private_key) # Load private key and sign message
return signer.sign(digest)
def verify (digest, signedBlockdata, publickeyfile):
public_key = False
with open(publickeyfile, "r") as public_key_file: # Load public key
public_key = RSA.importKey(public_key_file.read())
verifier = PKCS1_v1_5.new(public_key) # Verify message
verified = verifier.verify(digest, signedBlockdata)
assert verified, 'Signature verification failed'
print 'Successfully verified message\n'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment