from Crypto.PublicKey import RSA import json from Crypto.Cipher import PKCS1_OAEP from Crypto.Signature import PKCS1_v1_5 from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5 from Crypto import Random from base64 import b64encode, b64decode hash = "SHA-256" # def newkeys(keysize): # random_generator = Random.new().read # key = RSA.generate(keysize, random_generator) # private, public = key, key.publickey() # return public, private # def importKey(externKey): # return RSA.importKey(externKey) # def getpublickey(priv_key): # return priv_key.publickey() import requests from jwt import ( JWT, jwk_from_dict, jwk_from_pem, ) from jwt.utils import get_int_from_datetime from datetime import datetime, timedelta, timezone def create_hashed_transaction(): pass def create_jwt(): instance = JWT() message = { 'tha': create_hashed_transaction(), 'iat': get_int_from_datetime(datetime.now(timezone.utc)), 'exp': get_int_from_datetime( datetime.now(timezone.utc) + timedelta(hours=1)), } with open('rsa_private_key.pem', 'rb') as fh: signing_key = jwk_from_pem(fh.read()) compact_jws = instance.encode(message, signing_key, alg='RS256') return compact_jws def post_register(): credentials = { "c": "", "iv": "", "key": "" } response = requests.post("localhost:8080/register", data=credentials) def post_transaction_from_bank(): body = { "by": "{my_public_key}", "source": "{bank_public_key}", "target": "{my_public_key}", "amount": 0, "timestamp": get_int_from_datetime(datetime.now(timezone.utc)), } header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {create_jwt()}'} response = requests.post("localhost:8080/transaction", headers=header, data=body) print(response.headers) print(response.content) def post_transaction_to_user(): body = { "by": "{my_public_key}", "source": "{my_public_key}", "target": "{user_public_key}", "amount": 0, "timestamp": get_int_from_datetime(datetime.now(timezone.utc)), } header = {'Content-Type': 'application/json', 'Authorization': f'Bearer {create_jwt()}'} response = requests.post("localhost:8080/transaction", headers=header, data=body) def post_block(): credentials = { "c": "", "iv": "", "key": "" } response = requests.post("localhost:8080/register", params=credentials) def get_transaction(): response = requests.get("http://localhost:8080/transaction") print(response.headers) def get_block(): response = requests.get("http://localhost:8080/block") print(response.headers) def encrypt(message, pub_key): cipher = PKCS1_OAEP.new(pub_key) return cipher.encrypt(message) if __name__ == "__main__": myself = { "student_id": "e2482057", "public_key": """-----BEGIN PUBLIC KEY----- MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA3agASpH/TplIAX0YBqmh 5q3Iq6+LcJtlUVWiI/v0T74XwYPZaJpAArHaiMUGXAWxmzfbvEo1wE9RzySYV/5k QSpYDRekpOn0flIAQHORVbJ08s0udH6/c2AyAzqiwZbR1DRr7M90pSLvWvzHQT+c kT6rXYcp9GlSAv3AXRw5ZYalbQf7ST/Mb4T8O1MRkAatzXg3T4x3XJ3uxHOletLL SzsfY52kEn0uaFG6UI7UG50h8jcjqBxn+ETbn2YEZG5ecmPdYNakq2pqrdWXWMhE AUd927qkxgg/nyyFqwxIbTxebxzpNX8IHMT8PgNdMxVMqnhBWxiw1nborY+pwGVL MwIDAQAB -----END PUBLIC KEY-----""" } # with open("../secrets/gradecoin.pub", "r") as fs: # data = fs.read() # pubkeyobj = RSA.importKey(data) # # cipher = PKCS1_OAEP.new(pubkeyobj) # # ser = json.dumps(myself, separators=(',', ':')) # # a = cipher.encrypt(ser) # # print(f"{a}") get_block() get_transaction()