| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import os
- import rsa
- from cryptography.hazmat.backends import default_backend
- #from cryptography.hazmat.primitives.asymmetric import rsa
- from cryptography.hazmat.primitives import serialization
- from cryptography.fernet import Fernet
- def make_keys(contract_name):
- # create pubkey and private key for new contract
- (public_key,private_key)=rsa.newkeys(2048)
- # save them in contract folder
- path = f'contract_keys\\{contract_name}\\'
- os.makedirs(path, exist_ok=True)
-
- # write pubkey
- with open(path + 'publickey.key','wb') as f:
- f.write(public_key.save_pkcs1('PEM'))
- # write private key
- with open(path + 'privatekey.key','wb') as f:
- f.write(private_key.save_pkcs1('PEM'))
-
- # create semmetric key
- symmetric_key = Fernet.generate_key()
- with open(path + 'symmetric.key','wb') as f:
- f.write(symmetric_key)
- # retrieve public key
- def get_keys(contract_name):
- path = 'contract_keys\\' + contract_name + '\\publickey.key'
- # retreive public key form path
- with open(path, 'rb') as f:
- return rsa.PublicKey.load_pkcs1(f.read())
- # retrieve symmetric key
- def get_symmetric_key(contract_name):
- path = 'contract_keys\\' + contract_name + '\\symmetric.key'
- # read semmetric key from file
- with open(path, 'rb') as f:
- return f.read()
- # get private key
- def get_private_key(contract_name):
- path = 'contract_keys\\' + contract_name + '\\privatekey.key'
- # read private key from file
- with open(path, 'rb') as f:
- return rsa.PrivateKey.load_pkcs1(f.read())
- # get key in plain text (only public key)
- def get_plain_key(contract_name):
- path = 'contract_keys\\' + contract_name + '\\publickey.key'
- # read file and remove unnecessary suf
- with open(path, 'rt') as f:
- return f.read().split('-----')[2].strip().replace('\n', '')
|