From 425eb24854f1c3397aaaba61fa1cf71c76b27c4b Mon Sep 17 00:00:00 2001 From: Justin Simon Date: Sun, 7 May 2017 04:39:47 -0500 Subject: Support signing a pre-calculated hash (#87) * Split the hashing out of the sign method This code change adds support to split the hashing of a message and the actual signing of the message. * Updating unit test and documentation This commit updates the unit test and usage docs. In addition, This change removes a redundant error check inside rsa.sign(). * Refactore unit tests and code comments Removed the print statements from the unit test and refactored a few code comments to improve readability. * Rename hash function The new hash function had the same name as a function in the standard library. This commit changes the name to avoid conflicts. * Rename hash function to compute_hash() This commit renames the hash function to compute_hash(). --- tests/test_pkcs1.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'tests') diff --git a/tests/test_pkcs1.py b/tests/test_pkcs1.py index a8afea7..a96c8be 100644 --- a/tests/test_pkcs1.py +++ b/tests/test_pkcs1.py @@ -111,3 +111,24 @@ class SignatureTest(unittest.TestCase): signature2 = pkcs1.sign(message, self.priv, 'SHA-1') self.assertEqual(signature1, signature2) + + def test_split_hash_sign(self): + """Hashing and then signing should match with directly signing the message. """ + + message = b'je moeder' + msg_hash = pkcs1.compute_hash(message, 'SHA-256') + signature1 = pkcs1.sign_hash(msg_hash, self.priv, 'SHA-256') + + # Calculate the signature using the unified method + signature2 = pkcs1.sign(message, self.priv, 'SHA-256') + + self.assertEqual(signature1, signature2) + + def test_hash_sign_verify(self): + """Test happy flow of hash, sign, and verify""" + + message = b'je moeder' + msg_hash = pkcs1.compute_hash(message, 'SHA-256') + signature = pkcs1.sign_hash(msg_hash, self.priv, 'SHA-256') + + self.assertTrue(pkcs1.verify(message, signature, self.pub)) -- cgit v1.2.1