diff options
author | Sybren A. Stüvel <sybren@stuvel.eu> | 2017-04-10 12:12:29 +0200 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2017-04-10 12:12:29 +0200 |
commit | 395b8d661566f15632b0fb0e36fb8ea453ad9bf3 (patch) | |
tree | c200069ad7eeca8868244fbea2f5a979c0f98ed8 /rsa | |
parent | a478d4c23a75c76f1e5587b3c08b1f3a1f2cbf8e (diff) | |
download | rsa-git-395b8d661566f15632b0fb0e36fb8ea453ad9bf3.tar.gz |
Feature request #78: Expose function to find the hash method of a signature
I've not used the name "find_method_hash" suggested in #78, as it's a bit
vague. It's ok-ish for a private function `_find_method_hash`, but I
thought `find_signature_hash` would be more descriptive.
Diffstat (limited to 'rsa')
-rw-r--r-- | rsa/__init__.py | 2 | ||||
-rw-r--r-- | rsa/pkcs1.py | 22 |
2 files changed, 22 insertions, 2 deletions
diff --git a/rsa/__init__.py b/rsa/__init__.py index e69bbf3..95cd3fd 100644 --- a/rsa/__init__.py +++ b/rsa/__init__.py @@ -25,7 +25,7 @@ prevent repetitions, or other common security improvements. Use with care. from rsa.key import newkeys, PrivateKey, PublicKey from rsa.pkcs1 import encrypt, decrypt, sign, verify, DecryptionError, \ - VerificationError + VerificationError, find_signature_hash __author__ = "Sybren Stuvel, Barry Mead and Yesudeep Mangalapilly" __date__ = "2016-03-29" diff --git a/rsa/pkcs1.py b/rsa/pkcs1.py index bb08f4d..41f543c 100644 --- a/rsa/pkcs1.py +++ b/rsa/pkcs1.py @@ -294,6 +294,7 @@ def verify(message, signature, pub_key): :param signature: the signature block, as created with :py:func:`rsa.sign`. :param pub_key: the :py:class:`rsa.PublicKey` of the person signing the message. :raise VerificationError: when the signature doesn't match the message. + :returns: the name of the used hash. """ @@ -314,7 +315,26 @@ def verify(message, signature, pub_key): if expected != clearsig: raise VerificationError('Verification failed') - return True + return method_name + + +def find_signature_hash(signature, pub_key): + """Returns the hash name detected from the signature. + + If you also want to verify the message, use :py:func:`rsa.verify()` instead. + It also returns the name of the used hash. + + :param signature: the signature block, as created with :py:func:`rsa.sign`. + :param pub_key: the :py:class:`rsa.PublicKey` of the person signing the message. + :returns: the name of the used hash. + """ + + keylength = common.byte_size(pub_key.n) + encrypted = transform.bytes2int(signature) + decrypted = core.decrypt_int(encrypted, pub_key.e, pub_key.n) + clearsig = transform.int2bytes(decrypted, keylength) + + return _find_method_hash(clearsig) def yield_fixedblocks(infile, blocksize): |