Add serialization methods for the keystore keypairs

pull/84/head
tuxxy 2017-10-20 15:32:51 -06:00
parent 84b9375179
commit 0855bd5972
No known key found for this signature in database
GPG Key ID: 7BB971A0E89144D9
1 changed files with 45 additions and 0 deletions

View File

@ -3,6 +3,7 @@ from typing import Tuple
from nacl.secret import SecretBox from nacl.secret import SecretBox
from nkms.crypto import api as API from nkms.crypto import api as API
from nkms.keystore import constants
from npre import umbral from npre import umbral
from npre import elliptic_curve as ec from npre import elliptic_curve as ec
@ -81,6 +82,28 @@ class EncryptingKeypair(Keypair):
cipher = SecretBox(key) cipher = SecretBox(key)
return cipher.decrypt(edata) return cipher.decrypt(edata)
def serialize_pubkey(self) -> bytes:
"""
Serializes the pubkey for storage.
:return: The serialized pubkey in bytes
"""
serialized_key = (constants.ENC_KEYPAIR_BYTE +
constants.PUB_KEY_BYTE +
self.pubkey)
return serialized_key
def serialize_privkey(self) -> bytes:
"""
Serializes the privkey for storage.
:return: The serialized privkey in bytes
"""
serialized_key = (constants.ENC_KEYPAIR_BYTE +
constants.PRIV_KEY_BYTE +
self.privkey)
return serialized_key
class SigningKeypair(Keypair): class SigningKeypair(Keypair):
""" """
@ -126,3 +149,25 @@ class SigningKeypair(Keypair):
""" """
v, r, s = API.ecdsa_load_sig(signature) v, r, s = API.ecdsa_load_sig(signature)
return API.ecdsa_verify(v, r, s, msghash, self.pubkey) return API.ecdsa_verify(v, r, s, msghash, self.pubkey)
def serialize_pubkey(self) -> bytes:
"""
Serializes the pubkey for storage.
:return: The serialized pubkey in bytes
"""
serialized_key = (constants.SIG_KEYPAIR_BYTE +
constants.PUB_KEY_BYTE +
self.pubkey)
return serialized_key
def serialize_privkey(self) -> bytes:
"""
Serializes the privkey for storage.
:return: The serialized privkey in bytes
"""
serialized_key = (constants.SIG_KEYPAIR_BYTE +
constants.PRIV_KEY_BYTE +
self.privkey)
return serialized_key