Skip to content
Snippets Groups Projects
Commit 0667f390 authored by Donald Stufft's avatar Donald Stufft
Browse files

Add an Encodable mixin that uses the encoder system

parent 2a7758c1
No related branches found
No related tags found
No related merge requests found
...@@ -28,6 +28,13 @@ class Encoder(object): ...@@ -28,6 +28,13 @@ class Encoder(object):
encoder = Encoder() encoder = Encoder()
class Encodable(object):
def encode(self, encoding="raw"):
data = bytes(self)
return encoder[encoding].encode(data)
@encoder.register("raw") @encoder.register("raw")
class RawEncoder(object): class RawEncoder(object):
......
...@@ -4,7 +4,7 @@ from __future__ import division ...@@ -4,7 +4,7 @@ from __future__ import division
from . import six from . import six
from . import nacl from . import nacl
from .encoding import encoder from .encoding import encoder, Encodable
from .exceptions import CryptoError from .exceptions import CryptoError
from .random import random from .random import random
...@@ -42,7 +42,7 @@ class SignedMessage(six.binary_type): ...@@ -42,7 +42,7 @@ class SignedMessage(six.binary_type):
return self._message return self._message
class VerifyKey(object): class VerifyKey(Encodable, six.StringFixer, object):
""" """
The public key counterpart to an Ed25519 SigningKey for producing digital The public key counterpart to an Ed25519 SigningKey for producing digital
signatures. signatures.
...@@ -60,6 +60,9 @@ class VerifyKey(object): ...@@ -60,6 +60,9 @@ class VerifyKey(object):
self._key = key self._key = key
def __bytes__(self):
return self._key
def verify(self, smessage, signature=None, encoding="raw"): def verify(self, smessage, signature=None, encoding="raw"):
""" """
Verifies the signature of a signed message, returning the message Verifies the signature of a signed message, returning the message
...@@ -89,7 +92,7 @@ class VerifyKey(object): ...@@ -89,7 +92,7 @@ class VerifyKey(object):
return nacl.ffi.buffer(message, message_len[0])[:] 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. Private key for producing digital signatures using the Ed25519 algorithm.
...@@ -130,6 +133,9 @@ class SigningKey(object): ...@@ -130,6 +133,9 @@ class SigningKey(object):
# Public values # Public values
self.verify_key = VerifyKey(nacl.ffi.buffer(pk, nacl.lib.crypto_sign_PUBLICKEYBYTES)[:]) self.verify_key = VerifyKey(nacl.ffi.buffer(pk, nacl.lib.crypto_sign_PUBLICKEYBYTES)[:])
def __bytes__(self):
return self._seed
@classmethod @classmethod
def generate(cls): def generate(cls):
""" """
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment