From 0667f390e367105f5056c462b929de454ce7d8d2 Mon Sep 17 00:00:00 2001 From: Donald Stufft <donald@stufft.io> Date: Sat, 9 Mar 2013 20:21:32 -0500 Subject: [PATCH] Add an Encodable mixin that uses the encoder system --- nacl/encoding.py | 7 +++++++ nacl/signing.py | 12 +++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/nacl/encoding.py b/nacl/encoding.py index 92db5ffd..48c96ea1 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 b99001a2..1711f6ac 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): """ -- GitLab