diff --git a/nacl/encoding.py b/nacl/encoding.py index 92db5ffd2cab0b2a2bbb0f1507e6026dbba732ba..48c96ea187466e0d8c0c80b89d6e81c7062802f6 100644 --- a/nacl/encoding.py +++ b/nacl/encoding.py @@ -28,6 +28,13 @@ class Encoder(object): encoder = Encoder() +class Encodable(object): + + def encode(self, encoding="raw"): + data = bytes(self) + return encoder[encoding].encode(data) + + @encoder.register("raw") class RawEncoder(object): diff --git a/nacl/signing.py b/nacl/signing.py index b99001a29d9c3dc87f01f4b1189c0b142369ec5e..1711f6acab9b07b53cfb43074248ac4328350e53 100644 --- a/nacl/signing.py +++ b/nacl/signing.py @@ -4,7 +4,7 @@ from __future__ import division from . import six from . import nacl -from .encoding import encoder +from .encoding import encoder, Encodable from .exceptions import CryptoError from .random import random @@ -42,7 +42,7 @@ class SignedMessage(six.binary_type): return self._message -class VerifyKey(object): +class VerifyKey(Encodable, six.StringFixer, object): """ The public key counterpart to an Ed25519 SigningKey for producing digital signatures. @@ -60,6 +60,9 @@ class VerifyKey(object): self._key = key + def __bytes__(self): + return self._key + def verify(self, smessage, signature=None, encoding="raw"): """ Verifies the signature of a signed message, returning the message @@ -89,7 +92,7 @@ class VerifyKey(object): return nacl.ffi.buffer(message, message_len[0])[:] -class SigningKey(object): +class SigningKey(Encodable, six.StringFixer, object): """ Private key for producing digital signatures using the Ed25519 algorithm. @@ -130,6 +133,9 @@ class SigningKey(object): # Public values self.verify_key = VerifyKey(nacl.ffi.buffer(pk, nacl.lib.crypto_sign_PUBLICKEYBYTES)[:]) + def __bytes__(self): + return self._seed + @classmethod def generate(cls): """