UmbralPublicKeys allow hex serialization

pull/241/head
David Núñez 2019-03-27 16:23:24 +01:00
parent 7568fff780
commit 2c3620249b
2 changed files with 18 additions and 0 deletions

View File

@ -133,6 +133,14 @@ def test_key_encoder_decoder(random_ec_curvebn1):
assert decoded_key.to_bytes() == umbral_key.to_bytes()
def test_public_key_as_hex(random_ec_curvebn1):
pubkey = UmbralPrivateKey(random_ec_curvebn1, default_params()).get_pubkey()
hex_string = pubkey.hex()
decoded_pubkey = UmbralPublicKey.from_hex(hex_string)
assert pubkey == decoded_pubkey
def test_umbral_key_to_cryptography_keys():
umbral_priv_key = UmbralPrivateKey.gen_key()
umbral_pub_key = umbral_priv_key.get_pubkey()

View File

@ -321,6 +321,16 @@ class UmbralPublicKey:
return umbral_pubkey
def hex(self, is_compressed: bool = True) -> str:
"""
Returns an Umbral public key as hex string.
"""
return self.to_bytes(is_compressed=is_compressed).hex()
@classmethod
def from_hex(cls, hex_string: str) -> 'UmbralPublicKey':
return cls.from_bytes(key_bytes=hex_string, decoder=bytes.fromhex)
def to_cryptography_pubkey(self) -> _EllipticCurvePublicKey:
"""
Returns a cryptography.io EllipticCurvePublicKey from the Umbral key.