Skip to content
This repository has been archived by the owner on Sep 10, 2019. It is now read-only.

Commit

Permalink
Merge 9cb2797 into d61ed6e
Browse files Browse the repository at this point in the history
  • Loading branch information
jseagrave21 committed Mar 21, 2019
2 parents d61ed6e + 9cb2797 commit 7a1fba5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions neocore/Cryptography/Crypto.py
Expand Up @@ -112,6 +112,7 @@ def Sign(message, private_key):
Returns:
bytearray: the signature of the message.
"""
Crypto.SetupSignatureCurve()

hash = hashlib.sha256(binascii.unhexlify(message)).hexdigest()

Expand Down
37 changes: 37 additions & 0 deletions tests/test_cryptography.py
Expand Up @@ -265,3 +265,40 @@ def test_faulty_message_param_to_verify_signature(self):

result = Crypto.VerifySignature(faulty_message, fake_signature, fake_pubkey)
self.assertFalse(result)


class TestSigningWithoutCryptoInstance(TestCase):
def setUp(self):
import bitcoin
# reset Elliptic curve parameters to secp256k1
bitcoin.change_curve(bitcoin.P, bitcoin.N, bitcoin.A, bitcoin.B, bitcoin.Gx, bitcoin.Gy)

sig1 = None

def test_normal_signing(self):
# test the normal order of operations: Keypair will initialize secp256r1 Elliptic curve parameters
message = binascii.hexlify(b'Hello World')
key = KeyPair(bytes.fromhex('8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4'))
signature = Crypto.Sign(message, '8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4')

self.assertTrue(Crypto.VerifySignature(message, signature, key.PublicKey))
TestSigningWithoutCryptoInstance.sig1 = signature.hex()

def tearDown(self):
import bitcoin
# reset Elliptic curve parameters to secp256k1
bitcoin.change_curve(bitcoin.P, bitcoin.N, bitcoin.A, bitcoin.B, bitcoin.Gx, bitcoin.Gy)

sig2 = None

def test_signing_without_cyptoinstance(self):
# test signing prior to initializing secp256r1 Elliptic curve parameters with Keypair
message = binascii.hexlify(b'Hello World')
signature = Crypto.Sign(message, '8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4')
key = KeyPair(bytes.fromhex('8631cd2635c416ba5f043561e9d2ff40b79c3bb2eb245e176615298b8372d0a4'))

self.assertTrue(Crypto.VerifySignature(message, signature, key.PublicKey))
TestSigningWithoutCryptoInstance.sig2 = signature.hex()

# ensure the signatures are identical
self.assertEqual(TestSigningWithoutCryptoInstance.sig1, TestSigningWithoutCryptoInstance.sig2)

0 comments on commit 7a1fba5

Please sign in to comment.