def test_sign_and_verify(self): privkey = KeyPair.PrivateKeyFromWIF( "L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP") keypair = KeyPair(privkey) hashdata = b'aabbcc' keypair_signature = Crypto.Sign(hashdata, bytes(keypair.PrivateKey)) keypair_signature2 = Crypto.Default().Sign(hashdata, bytes(keypair.PrivateKey)) self.assertEqual(keypair_signature, keypair_signature2) verification_result = Crypto.VerifySignature(hashdata.decode('utf8'), keypair_signature, keypair.PublicKey) verification_result2 = Crypto.Default().VerifySignature( hashdata.decode('utf8'), keypair_signature, keypair.PublicKey) self.assertEqual(verification_result, verification_result2) self.assertTrue(verification_result) # verify with compressed key verification_result3 = Crypto.VerifySignature( hashdata.decode('utf8'), keypair_signature, binascii.unhexlify(keypair.PublicKey.encode_point(True))) self.assertTrue(verification_result3) # this should fail because the signature will not match the input data verification_result = Crypto.VerifySignature(b'aabb', keypair_signature, keypair.PublicKey) self.assertFalse(verification_result)
def test_faulty_message_param_to_verify_signature(self, mocked_logger): faulty_message = bytes.fromhex( 'aa' ) # faulty because the message should be non-raw bytes. i.e. b'aa' fake_signature = bytes.fromhex('aabb') # irrelevant for the test fake_pubkey = bytes.fromhex('aabb') # irrelevant for the test result = Crypto.VerifySignature(faulty_message, fake_signature, fake_pubkey) self.assertTrue(mocked_logger.error.called) self.assertFalse(result)
def verify_signature(message, signature, pubkey): signature = binascii.unhexlify(signature.encode()) pubkey = binascii.unhexlify(pubkey.encode()) result = Crypto.VerifySignature(message, signature, pubkey) return result