def generate_KeyPair(): privateKey = PrivateKey() publicKey = privateKey.publicKey() a = str(privateKey.toPem()) b = str(publicKey.toPem()) a = a.replace('-----BEGIN EC PRIVATE KEY-----\n', '') a = a.replace('\n-----END EC PRIVATE KEY-----\n', '') b = b.replace('-----BEGIN PUBLIC KEY-----\n', '') b = b.replace('\n-----END PUBLIC KEY-----\n', '') return a, b
def ppk_keygen(): ''' PEM (string) DER Bytes formats ''' if config.DIGITAL_SIGNATURE_ALGO == 'ECDSA': # https://github.com/starkbank/ecdsa-python ''' MUST use toPem.... private_key = PrivateKey() private_key.toPem() == PrivateKey.fromPem(private_key.toPem()).toPem() type(private_key.toPem()) == str public_key.toPem() == PublicKey.fromPem(public_key.toPem()).toPem() ''' private_key = PrivateKey() public_key = private_key.publicKey() private_key, public_key = (private_key.toPem(), public_key.toPem()) elif config.DIGITAL_SIGNATURE_ALGO == 'SCHNORR': ''' We guess some private keys that satisfies certain properties.. ie. it's on the curve ''' trying = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 n = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 trying_byte = trying.to_bytes(32, byteorder="big") n_byte = n.to_bytes(32, byteorder="big") # convert msg to BYTE trying_int = schnorr.int_from_bytes(trying_byte) n_int = schnorr.int_from_bytes(n_byte) while (trying_int >= n_int - 1): trying = os.urandom(32) # trying is bytes trying_int = schnorr.int_from_bytes( trying) # convert bytes to integer private_key = trying public_key = schnorr.pubkey_gen(private_key) public_key, private_key = (bytes_to_hex(public_key), bytes_to_hex(private_key)) else: log_error( "[security.ppk_keygen.ppk_keygen] Unknown keygen algo defined") return public_key, private_key
def create(path=None): """# Generate a new key pair Generates a secp256k1 ECDSA private/public key pair to be used in the API authentications ## Parameters (optional): path [string]: path to save the keys .pem files. No files will be saved if this parameter isn't provided ## Return: private and public key pems """ private = PrivateKey() public = private.publicKey() private_pem = private.toPem() public_pem = public.toPem() if path is not None: with open(os_path.join(path, "private-key.pem"), "w") as file: file.write(private_pem) with open(os_path.join(path, "public-key.pem"), "w") as file: file.write(public_pem) return private_pem, public_pem
def generate_keys(self): pr_key = PrivateKey() self.private_key = str(pr_key.toPem()) self.public_key = str(pr_key.publicKey().toPem())
def generate_KeyPair(): privateKey = PrivateKey() publicKey = privateKey.publicKey() return privateKey.toPem(), publicKey.toPem()
def testPemConversion(self): privateKey1 = PrivateKey() pem = privateKey1.toPem() privateKey2 = PrivateKey.fromPem(pem) self.assertEqual(privateKey1.secret, privateKey2.secret) self.assertEqual(privateKey1.curve, privateKey2.curve)