def generate_keys(self, save=False, filen='default_key'): """ Generate new Private and Public key pair. (optional) save keys in a pem file :param save: (optional) False to not save the new generated private/public key pair, True to save them :type save: Boolean :param filen: (optional) is the pem file name of key to load :type filen: str :return: None """ self.private_key, self.public_key = keys.gen_keypair(secp256k1) if save: keys.export_key(self.private_key, curve=secp256k1, filepath=self.path + 'cryp/.private/' + filen + 'prv.pem') keys.export_key(self.public_key, curve=secp256k1, filepath=self.path + 'cryp/public/' + filen + 'pub.pem')
def save_keys_to_file(self): try: self.validate_keys() fname = input('write a file name : ') + '.private' export_key(self._private_key, curve=curve.P256, filepath=fname) print('private and public keys were successfully saved to ', fname) except Exception as err: print('Keys saving failed : ', err)
def saveToFile(self, folder = ""): for (key, pub) in self.keys: s = tobytes(key, 32) hash = sha256() hash.update(s) filename = hash.hexdigest()[:8] filename = KEYFOLDER + folder + filename + ".key" keys.export_key(key, curve=curve.secp256k1, filepath=filename)
def generate_key_pair(self): pvt, pub = gen_keypair(curve.secp256k1) export_key(pvt, curve=curve.secp256k1, filepath='/home/zatosh/keys/secp256k1.key') export_key(pub, curve=curve.secp256k1, filepath='/home/zatosh/keys/secp256k1.pub') return True
def keyGen(username, type): privateKeyFile = username + '_privkey.pem' publicKeyFile = username + '_pubkey.pem' privateKey = keys.gen_private_key(curve.P256) publicKey = keys.get_public_key(privateKey, curve.P256) keys.export_key(privateKey, curve=curve.P256, filepath="keys/" + type + "Key/" + privateKeyFile) keys.export_key(publicKey, curve=curve.P256, filepath="keys/" + type + "Key/" + publicKeyFile) return True
def test_generate_and_parse_pem(self): d, Q = gen_keypair(P256) export_key(d, curve=P256, filepath='p256.key') export_key(Q, curve=P256, filepath='p256.pub') parsed_d, parsed_Q = import_key('p256.key') self.assertEqual(parsed_d, d) self.assertEqual(parsed_Q, Q) parsed_d, parsed_Q = import_key('p256.pub') self.assertTrue(parsed_d is None) self.assertEqual(parsed_Q, Q) remove('p256.key') remove('p256.pub')
def generate_secp256r1_key_pairs(save_path): """Generate the key pairs of secp256r1 :param str save_path: The save file path of private key :rtype:tuple :return: The hex string of public key """ private_key, public_key = keys.gen_keypair(curve.P256) keys.export_key(private_key, curve.P256, filepath=save_path) x = str(hex(public_key.x))[2:] y = str(hex(public_key.y))[2:] if len(x) < 64: x = '0' * (64 - len(x)) + x if len(y) < 64: y = '0' * (64 - len(y)) + y public_key = '0x' + x + y return public_key
def export_keys(self, filen='default_key'): """ Save keys to disk. :param filen: (optional) is the pem file name of key to load :type filen: str :return: None """ if not self.private_key and not self.public_key: raise TypeError("no key to export") if self.private_key: keys.export_key(self.private_key, curve=secp256k1, filepath=self.path + 'cryp/.private/' + filen + 'prv.pem') if self.public_key: keys.export_key(self.public_key, curve=secp256k1, filepath=self.path + 'cryp/public/' + filen + 'pub.pem')
from fastecdsa import curve, ecdsa, keys from hashlib import sha384 m = "a message to sign via ECDSA" # some message ''' use default curve and hash function (P256 and SHA2) ''' private_key = keys.gen_private_key(curve.P256) public_key = keys.get_public_key(private_key, curve.P256) print("private key: ",private_key) print("public key: ",public_key) # standard signature, returns two integers r, s = ecdsa.sign(m, private_key) # should return True as the signature we just generated is valid. valid = ecdsa.verify((r, s), m, public_key) print("Is it valid? = ",valid) # save the private key to disk keys.export_key(private_key, curve=curve.P256, filepath='./p256.key') # save the public key to disk keys.export_key(public_key, curve=curve.P256, filepath='./p256.pub')
def add_wallet_to_db(port: str, wallet: "Wallet"): location = WALLET_DB_LOC + str(port) export_key(wallet.private_key, curve=secp256k1, filepath=location + ".key") with open(location + ".pub", "w") as file: file.write(wallet.public_key)
def to_bytes(self) -> bytes: return keys.export_key(self.impl, self.curve)
def serialize_pub_key(pub_key): return keys.export_key(pub_key, curve=curve.secp256k1)
def generate_keys(): priv_key = keys.gen_private_key(curve.secp256k1) pub_key = keys.get_public_key(priv_key,curve.secp256k1) keys.export_key(priv_key,curve=curve.secp256k1,filepath='./priv_key.key') keys.export_key(pub_key,curve=curve.secp256k1,filepath='./pub_key.pub')
def pubkey(s): "export the public key only" return fe_keys.export_key(s._pubkey, curve=s.CURVE)
def export(self): "export the private key or public key, as the case may be, as PEM" if self._key is not None: return fe_keys.export_key(self._key, curve=self.CURVE) return fe_keys.export_key(self._pubkey, curve=self.CURVE)
def export_keys(self, path): export_key(self.private_key, curve.secp256k1, path) export_key(self.public_key, curve.secp256k1, path)
from fastecdsa import curve, ecdsa, keys from hashlib import sha384 import sys m = "a message to sign via ECDSA" # some message ''' use default curve and hash function (P256 and SHA2) ''' private_key = keys.gen_private_key(curve.P256) public_key = keys.get_public_key(private_key, curve.P256) print("private key: ",private_key) print("public key: ",public_key) # standard signature, returns two integers r, s = ecdsa.sign(m, private_key) # should return True as the signature we just generated is valid. valid = ecdsa.verify((r, s), m, public_key) print("Is it valid? = ",valid) # save the private key to disk keys.export_key(private_key, curve=curve.P256, filepath='./'+ sys.argv[1]+ '.key') # save the public key to disk keys.export_key(public_key, curve=curve.P256, filepath='./'+ sys.argv[1]+ '.pub')