mirror of https://github.com/nucypher/nucypher.git
Merge pull request #67 from tuxxy/iterable-signing
Allow iterables to be digested and signedpull/75/head
commit
4dfb4c4b59
|
@ -1,7 +1,9 @@
|
||||||
import msgpack
|
import msgpack
|
||||||
|
import sha3
|
||||||
from random import SystemRandom
|
from random import SystemRandom
|
||||||
from py_ecc.secp256k1 import N, privtopub, ecdsa_raw_sign, ecdsa_raw_recover
|
from py_ecc.secp256k1 import N, privtopub, ecdsa_raw_sign, ecdsa_raw_recover
|
||||||
from npre import umbral
|
from npre import umbral
|
||||||
|
from typing import Iterable
|
||||||
|
|
||||||
|
|
||||||
class EncryptingKeypair(object):
|
class EncryptingKeypair(object):
|
||||||
|
@ -125,6 +127,20 @@ class SigningKeypair(object):
|
||||||
s = int.from_bytes(sig[2], byteorder='big')
|
s = int.from_bytes(sig[2], byteorder='big')
|
||||||
return (v, r, s)
|
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):
|
def sign(self, msghash):
|
||||||
"""
|
"""
|
||||||
Signs a hashed message and returns a msgpack'ed v, r, and s.
|
Signs a hashed message and returns a msgpack'ed v, r, and s.
|
||||||
|
|
|
@ -109,12 +109,12 @@ class KeyRing(object):
|
||||||
"""
|
"""
|
||||||
Signs a message and returns a signature with the keccak hash.
|
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
|
:rtype: bytestring
|
||||||
:return: Signature of message
|
: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)
|
return self.sig_keypair.sign(msg_digest)
|
||||||
|
|
||||||
def verify(self, message, signature, pubkey=None):
|
def verify(self, message, signature, pubkey=None):
|
||||||
|
|
|
@ -33,6 +33,12 @@ class TestSigningKeypair(unittest.TestCase):
|
||||||
pubkey=self.keypair_a.pub_key)
|
pubkey=self.keypair_a.pub_key)
|
||||||
self.assertTrue(verify_sig)
|
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):
|
class TestEncryptingKeypair(unittest.TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import asyncio
|
import asyncio
|
||||||
|
import unittest
|
||||||
|
|
||||||
from nkms.characters import Ursula, Alice
|
from nkms.characters import Ursula, Alice
|
||||||
from nkms.crypto.keyring import KeyRing
|
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)
|
policy_group.transmit(networky_stuff)
|
||||||
|
|
||||||
|
|
||||||
|
@unittest.skip(reason="Work in progress")
|
||||||
def test_alice_finds_ursula():
|
def test_alice_finds_ursula():
|
||||||
event_loop = asyncio.new_event_loop()
|
event_loop = asyncio.new_event_loop()
|
||||||
asyncio.set_event_loop(event_loop)
|
asyncio.set_event_loop(event_loop)
|
||||||
|
|
Loading…
Reference in New Issue