Merge pull request #67 from tuxxy/iterable-signing

Allow iterables to be digested and signed
pull/75/head
Justin Holmes 2017-10-03 17:07:59 -07:00 committed by GitHub
commit 4dfb4c4b59
4 changed files with 26 additions and 2 deletions

View File

@ -1,7 +1,9 @@
import msgpack
import sha3
from random import SystemRandom
from py_ecc.secp256k1 import N, privtopub, ecdsa_raw_sign, ecdsa_raw_recover
from npre import umbral
from typing import Iterable
class EncryptingKeypair(object):
@ -125,6 +127,20 @@ class SigningKeypair(object):
s = int.from_bytes(sig[2], byteorder='big')
return (v, r, s)
def digest(self, *args):
"""
Accepts an iterable containing bytes and digests it.
:param bytes *args: Data to hash
:rtype: bytes
:return: bytestring of digested data
"""
hash = sha3.keccak_256()
for arg in args:
hash.update(arg)
return hash.digest()
def sign(self, msghash):
"""
Signs a hashed message and returns a msgpack'ed v, r, and s.

View File

@ -109,12 +109,12 @@ class KeyRing(object):
"""
Signs a message and returns a signature with the keccak hash.
:param bytes message: Message to sign in bytes
:param Iterable message: Message to sign in an iterable of bytes
:rtype: bytestring
:return: Signature of message
"""
msg_digest = sha3.keccak_256(message).digest()
msg_digest = self.sig_keypair.digest(message)
return self.sig_keypair.sign(msg_digest)
def verify(self, message, signature, pubkey=None):

View File

@ -33,6 +33,12 @@ class TestSigningKeypair(unittest.TestCase):
pubkey=self.keypair_a.pub_key)
self.assertTrue(verify_sig)
def test_digest(self):
digest_a = self.keypair_a.digest(b'foo', b'bar')
digest_b = self.keypair_a.digest(b'foobar')
self.assertEqual(digest_a, digest_b)
class TestEncryptingKeypair(unittest.TestCase):
def setUp(self):

View File

@ -1,4 +1,5 @@
import asyncio
import unittest
from nkms.characters import Ursula, Alice
from nkms.crypto.keyring import KeyRing
@ -44,6 +45,7 @@ def test_alice_has_ursulas_public_key_and_uses_it_to_encode_policy_payload():
policy_group.transmit(networky_stuff)
@unittest.skip(reason="Work in progress")
def test_alice_finds_ursula():
event_loop = asyncio.new_event_loop()
asyncio.set_event_loop(event_loop)