t 02 finished from lesson 5

This commit is contained in:
Spekulaas 2023-09-26 20:55:17 +02:00
parent cfdcbe300d
commit 6afd899b5c
4 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,33 @@
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

View File

@ -0,0 +1,59 @@
#!/usr/bin/env python3
"""
Transaction Class - Definition of Input and Output
The goal of this tutorial is to learn how to create a simple class for transactions.
A transaction is composed of a list of Inputs and a list of outputs, and few methods.
Inputs: There are one or more inputs in a transactions which indicates the senders and the
amount from each sender.
Outputs:There are one or more outputs in a transactions which indicates the receivers and the
amount to each receiver.
Note: Senders and Recievers are addressed by their public keys.
add_input() and add_output() should add inputs and outputs to a transaction.
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 'Transactions_t.py' in your command line
Notes:
* do not change class structure or method signature to not break unit tests
"""
class Tx:
# TODO 1: Complete the method
# In this method, you should initialize the variables for inputs and ouputs
# of a transaction.
# Inputs and Ouputs should be defined as list, where a list of senders (and the sending amount),
# and a list of receivers (and the receiving amount) are stored.
def __init__(self):
# In this method, you should initialize the variables for inputs and ouputs of a trancaction.
# Inputs and Ouputs should be defined as list, where a list of senders (and the sending amount),
# and a list of receivers (and the receiving amount) are stored.
self.inputs = []
self.outputs = []
# TODO 2: Complete the method
def add_input(self, from_addr, amount):
# This method should add a new sender and the associated amount to the list of inputs
# An input is a list of senders (from_addr) and the amount sent by each sender (amount)
self.inputs.append([from_addr, amount])
# TODO 3: Complete the method
def add_output(self, to_addr, amount):
# This method should add a new receiver and the associated amount to the list of outputs
# An output is a list of receivers (to_addr) and the amount recived by each receiver (amount)
self.outputs.append([to_addr, amount])

View File

@ -0,0 +1,57 @@
"""
This test case will verify if the provided exercise solution by a student for the Transaction.py is correct.
"""
from Signature import *
from Transaction import *
def print_transaction(tx_name, tx):
print('---------------')
print(f'-- {tx_name}')
for (index, input_item) in enumerate(tx.inputs):
for key_name, prv_key, pbc_key in keys_list:
if pbc_key == input_item[0]:
payer = key_name
print(f'In[{index+1}]: {payer} sends {input_item[1]} coin')
for (index, output_item) in enumerate(tx.outputs):
for key_name, prv_key, pbc_key in keys_list:
if pbc_key == output_item[0]:
receiver = key_name
print(f'Out[{index+1}]: {receiver} receives {output_item[1]} coin')
print()
if __name__ == '__main__':
keys_list =[]
alex_prv, alex_pbc = generate_keys()
keys_list.append(('alex', alex_prv, alex_pbc))
mike_prv, mike_pbc = generate_keys()
keys_list.append(('mike', mike_prv, mike_pbc))
rose_prv, rose_pbc = generate_keys()
keys_list.append(('rose', rose_prv, rose_pbc))
mara_prv, mara_pbc = generate_keys()
keys_list.append(('mara', mara_prv, mara_pbc))
Tx1 = Tx()
Tx1.add_input(alex_pbc, 0.9)
Tx1.add_output(mike_pbc, 0.8)
print_transaction('Tx1', Tx1)
Tx2 = Tx()
Tx2.add_input(alex_pbc, 2.1)
Tx2.add_output(mike_pbc, 0.9)
Tx2.add_output(rose_pbc, 1.0)
print_transaction('Tx2', Tx2)
Tx2.add_input(mara_pbc, 0.7)
Tx2.add_input(mara_pbc, 1.1)
Tx2.add_input(mara_pbc, 1.5)
Tx2.add_output(mike_pbc, 1.9)
Tx2.add_output(rose_pbc, 0.2)
print_transaction('Tx2', Tx2)

View File

@ -0,0 +1,21 @@
---------------
-- Tx1
In[1]: alex sends 0.9 coin
Out[1]: mike receives 0.8 coin
---------------
-- Tx2
In[1]: alex sends 2.1 coin
Out[1]: mike receives 0.9 coin
Out[2]: rose receives 1.0 coin
---------------
-- Tx2
In[1]: alex sends 2.1 coin
In[2]: mara sends 0.7 coin
In[3]: mara sends 1.1 coin
In[4]: mara sends 1.5 coin
Out[1]: mike receives 0.9 coin
Out[2]: rose receives 1.0 coin
Out[3]: mike receives 1.9 coin
Out[4]: rose receives 0.2 coin