def get_key(self, fingerprint: bytes) -> Union[keypairs.EncryptingKeypair, keypairs.SigningKeypair]: """ Returns a key from the KeyStore. :param fingerprint: Fingerprint, in bytes, of key to return :return: Keypair of the returned key. """ key = self.session.query(Key).filter_by(fingerprint=fingerprint).first() if not key: raise KeyNotFound( "No key with fingerprint {} found.".format(fingerprint)) keypair_byte = key.key_data[0].to_bytes(1, 'big') key_type_byte = key.key_data[1].to_bytes(1, 'big') key = key.key_data[2:] if keypair_byte == constants.ENC_KEYPAIR_BYTE: if key_type_byte == constants.PUB_KEY_BYTE: return keypairs.EncryptingKeypair(pubkey=key) elif key_type_byte == constants.PRIV_KEY_BYTE: return keypairs.EncryptingKeypair(privkey=key) elif keypair_byte == constants.SIG_KEYPAIR_BYTE: if key_type_byte == constants.PUB_KEY_BYTE: return keypairs.SigningKeypair(pubkey=key) elif key_type_byte == constants.PRIV_KEY_BYTE: return keypairs.SigningKeypair(privkey=key)
def __init__(self, keypair: keypairs.EncryptingKeypair = None): """ Initalizes an EncryptingPower object for CryptoPower. """ self.keypair = keypair or keypairs.EncryptingKeypair() self.priv_key = self.keypair.privkey self.pub_key = self.keypair.pubkey
def test_gen_keypair_if_needed(): new_enc_keypair = keypairs.EncryptingKeypair() assert new_enc_keypair._privkey != None assert new_enc_keypair.pubkey != None new_sig_keypair = keypairs.SigningKeypair() assert new_sig_keypair._privkey != None assert new_sig_keypair.pubkey != None
def gen_ecies_keypair(self, gen_priv=True) -> keypairs.EncryptingKeypair: """ Generates an ECIES keypair. TODO: Initalize keypair with provided data. :param gen_priv: Generate private key or not? :return: ECIES encrypting keypair """ ecies_keypair = keypairs.EncryptingKeypair() if gen_priv: ecies_keypair.gen_privkey() return ecies_keypair
def test_policy_contract_sqlite_keystore(test_keystore): alice_keypair_sig = keypairs.SigningKeypair(generate_keys_if_needed=True) alice_keypair_enc = keypairs.EncryptingKeypair(generate_keys_if_needed=True) bob_keypair_sig = keypairs.SigningKeypair(generate_keys_if_needed=True) hrac = b'test' # Test add PolicyContract new_contract = test_keystore.add_policy_contract( datetime.utcnow(), b'test', hrac, alice_pubkey_sig=alice_keypair_sig.pubkey, alice_signature=b'test' ) # Test get PolicyContract query_contract = test_keystore.get_policy_contract(hrac) assert new_contract == query_contract # Test del PolicyContract test_keystore.del_policy_contract(hrac) with pytest.raises(keystore.NotFound): del_key = test_keystore.get_policy_contract(hrac)
def setUp(self): self.ecies_keypair = keypairs.EncryptingKeypair() self.ecdsa_keypair = keypairs.SigningKeypair()