Skip to content

Commit

Permalink
Merge pull request #99 from pyca/restore-some-docstrings
Browse files Browse the repository at this point in the history
Restore docstrings
  • Loading branch information
Brian Warner committed Aug 15, 2014
2 parents 61cc9f2 + c3cb404 commit 56e0e04
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/nacl/signing.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@


class SignedMessage(six.binary_type):
"""
A bytes subclass that holds a messaged that has been signed by a
:class:`SigningKey`.
"""

@classmethod
def _from_parts(cls, signature, message, combined):
Expand All @@ -32,14 +36,27 @@ def _from_parts(cls, signature, message, combined):

@property
def signature(self):
"""
The signature contained within the :class:`SignedMessage`.
"""
return self._signature

@property
def message(self):
"""
The message contained within the :class:`SignedMessage`.
"""
return self._message


class VerifyKey(encoding.Encodable, StringFixer, object):
"""
The public key counterpart to an Ed25519 SigningKey for producing digital
signatures.
:param key: [:class:`bytes`] Serialized Ed25519 public key
:param encoder: A class that is able to decode the `key`
"""

def __init__(self, key, encoder=encoding.RawEncoder):
# Decode the key
Expand All @@ -57,6 +74,19 @@ def __bytes__(self):
return self._key

def verify(self, smessage, signature=None, encoder=encoding.RawEncoder):
"""
Verifies the signature of a signed message, returning the message
if it has not been tampered with else raising
:class:`~nacl.signing.BadSignatureError`.
:param smessage: [:class:`bytes`] Either the original messaged or a
signature and message concated together.
:param signature: [:class:`bytes`] If an unsigned message is given for
smessage then the detached signature must be provded.
:param encoder: A class that is able to decode the secret message and
signature.
:rtype: :class:`bytes`
"""
if signature is not None:
# If we were given the message and signature separately, combine
# them.
Expand All @@ -69,6 +99,23 @@ def verify(self, smessage, signature=None, encoder=encoding.RawEncoder):


class SigningKey(encoding.Encodable, StringFixer, object):
"""
Private key for producing digital signatures using the Ed25519 algorithm.
Signing keys are produced from a 32-byte (256-bit) random seed value. This
value can be passed into the :class:`~nacl.signing.SigningKey` as a
:func:`bytes` whose length is 32.
.. warning:: This **must** be protected and remain secret. Anyone who knows
the value of your :class:`~nacl.signing.SigningKey` or it's seed can
masquerade as you.
:param seed: [:class:`bytes`] Random 32-byte value (i.e. private key)
:param encoder: A class that is able to decode the seed
:ivar: verify_key: [:class:`~nacl.signing.VerifyKey`] The verify
(i.e. public) key that corresponds with this signing key.
"""

def __init__(self, seed, encoder=encoding.RawEncoder):
# Decode the seed
Expand All @@ -92,12 +139,24 @@ def __bytes__(self):

@classmethod
def generate(cls):
"""
Generates a random :class:`~nacl.signing.SingingKey` object.
:rtype: :class:`~nacl.signing.SigningKey`
"""
return cls(
random(nacl.c.crypto_sign_SEEDBYTES),
encoder=encoding.RawEncoder,
)

def sign(self, message, encoder=encoding.RawEncoder):
"""
Sign a message using this key.
:param message: [:class:`bytes`] The data to be signed.
:param encoder: A class that is used to encode the signed message.
:rtype: :class:`~nacl.signing.SignedMessage`
"""
raw_signed = nacl.c.crypto_sign(message, self._signing_key)

signature = encoder.encode(raw_signed[:nacl.c.crypto_sign_BYTES])
Expand Down

0 comments on commit 56e0e04

Please sign in to comment.