#Drew Pasley
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives import hashes
from cryptography.exceptions import InvalidSignature
#hint: try to adopt code from https://cryptography.io/en/latest/hazmat/primitives/asymmetric/ec.html
#hint: try to use sample codes (i.e., code in the green boxes)
# finish the implementation
def generate_keys():
private_key = ec.generate_private_key(ec.SECP384R1())
public_key = private_key.public_key()
return private_key, public_key
# finish the implementation
def sign(private_key, digest):
signature = private_key.sign(
data,
ec.ECDSA(hashes.SHA256()))
return signature
# finish the implementation
def verify(signature, public_key, data):
try:
public_key.verify(signature, data, ec.ECDSA(hashes.SHA256()))
except InvalidSignature:
print("Invalid")
# put your test cases here
print("testing valid input(should have no output)")
if __name__ == '__main__':
prv, pub = generate_keys()
data = b"this is some data I'd like to sign"
sig = sign(prv, data)
verify(sig, pub, data)
print("testing invalid unencrypted message")
data_new = b"this is modified data"
verify(sig, pub, data_new)
print("testing of tampered signiture")
pr_fake, pu_fake = generate_keys()
sig_fake = sign(pr_fake, data)
verify(sig_fake, pub, data)
print("complete")