t01 finished from lesson 5
This commit is contained in:
parent
9e8df74b9a
commit
cfdcbe300d
57
period_1/05-transaction/501_T01_A01_Sign_Data/Signature.py
Normal file
57
period_1/05-transaction/501_T01_A01_Sign_Data/Signature.py
Normal file
@ -0,0 +1,57 @@
|
||||
|
||||
# This tutorial is already done in lesson 3
|
||||
# You can copy and paste the completed signature module
|
||||
|
||||
"""Asymmetric Cryptography -> Digital Signature: Tutorial 4
|
||||
|
||||
The goal of this tutorial is to learn how to sign and verify messages using asymmetric keys.
|
||||
In this implementation the passed message as an argument is a string that needs to be converted to a byte object.
|
||||
When signing a message the RSA sign-function requires a specific hash like SHA256, and padding such as PSS.
|
||||
Be aware that verification must use the same algorithms values to correctly verify the signature.
|
||||
|
||||
Your task is to:
|
||||
* locate the TODOs in this file
|
||||
* complete the missing part from the code
|
||||
* run the test of this tutorial located in same folder.
|
||||
|
||||
To test run 'Signature_t.py' in your command line
|
||||
|
||||
Notes:
|
||||
* do not change class structure or method signature to not break unit tests
|
||||
* visit this url for more information on this topic:
|
||||
https://cryptography.io/en/latest/hazmat/primitives/asymmetric/rsa/
|
||||
"""
|
||||
|
||||
from cryptography.exceptions import *
|
||||
from cryptography.hazmat.primitives.asymmetric import rsa
|
||||
from cryptography.hazmat.primitives import hashes
|
||||
from cryptography.hazmat.primitives.asymmetric import padding
|
||||
|
||||
def generate_keys():
|
||||
private_key = rsa.generate_private_key(public_exponent=65537,key_size=2048)
|
||||
public_key = private_key.public_key()
|
||||
return private_key, public_key
|
||||
|
||||
def sign(message, private_key):
|
||||
signature = private_key.sign(
|
||||
message,
|
||||
padding.PSS(mgf=padding.MGF1(hashes.SHA256()), salt_length=padding.PSS.MAX_LENGTH),
|
||||
hashes.SHA256()
|
||||
)
|
||||
return signature
|
||||
|
||||
def verify(message, signature, public_key):
|
||||
try:
|
||||
public_key.verify(
|
||||
signature,
|
||||
message,
|
||||
padding.PSS(mgf=padding.MGF1(hashes.SHA256()),
|
||||
salt_length=padding.PSS.MAX_LENGTH),
|
||||
hashes.SHA256()
|
||||
)
|
||||
return True
|
||||
except InvalidSignature:
|
||||
return False
|
||||
except:
|
||||
print('Error executing public_key.verify')
|
||||
return False
|
@ -0,0 +1,63 @@
|
||||
"""
|
||||
This test case will verify if the provided exercise solution by a student for the Signature.py is correct.
|
||||
|
||||
The goal of this tutorial is to learn how to create text-based unformatted transactions.
|
||||
However, in real scenario, we need to use a more specific and useful format for transactions (next tutorial).
|
||||
|
||||
|
||||
Your task is to:
|
||||
* locate the TODOs in this file
|
||||
* complete the missing part from the code
|
||||
* run this test file and observe the results.
|
||||
|
||||
"""
|
||||
|
||||
from Signature import *
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
alex_prv, alex_pbc = generate_keys()
|
||||
mike_prv, mike_pbc = generate_keys()
|
||||
|
||||
data = [
|
||||
'Alex pays 2 coin to mike',
|
||||
'Alex pays 1.2 coins to Mara',
|
||||
'Mike pays 0.6 coin to Alex'
|
||||
]
|
||||
|
||||
|
||||
|
||||
# TODO 1: Complete the test case 1
|
||||
# Create a test case to sign data using alex's signature
|
||||
# and then try to verify it using the same (alex's) signature
|
||||
# As data is signed by alex signature, it should be successfully verified by alex's key
|
||||
|
||||
# sign data with alexs signature
|
||||
alex_data = sign(data[0].encode(), alex_prv)
|
||||
|
||||
# verify data with alexs signature
|
||||
if verify(data[0].encode(), alex_data, alex_pbc):
|
||||
print('Success: Valid signature is verified.')
|
||||
else:
|
||||
print('Failed: Invalid signature is not verified.')
|
||||
|
||||
# Test case 1: write your code here:
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# TODO 2: Complete the test case 2
|
||||
# Create a test case to sign data using alex's signature
|
||||
# and then try to verify it using mike's signature
|
||||
# As data is signed by alex signature, it should not be successfully verified by mike's (or any other key) key
|
||||
|
||||
# Test case 2: write your code here:
|
||||
if verify(data[0].encode(), alex_data, mike_pbc):
|
||||
print('Failed: Valid signature is verified.')
|
||||
else:
|
||||
print('Success: Invalid signature is not verified.')
|
2
period_1/05-transaction/501_T01_A01_Sign_Data/output.txt
Normal file
2
period_1/05-transaction/501_T01_A01_Sign_Data/output.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Success: Valid signature is verified.
|
||||
Success: Inalid signature is not verified.
|
Loading…
x
Reference in New Issue
Block a user