diff options
-rw-r--r-- | lib/Crypto/SelfTest/Hash/__init__.py | 10 | ||||
-rw-r--r-- | lib/Crypto/SelfTest/Hash/test_HMAC.py | 14 | ||||
-rw-r--r-- | setup.py | 8 |
3 files changed, 27 insertions, 5 deletions
diff --git a/lib/Crypto/SelfTest/Hash/__init__.py b/lib/Crypto/SelfTest/Hash/__init__.py index b6e6053..d457519 100644 --- a/lib/Crypto/SelfTest/Hash/__init__.py +++ b/lib/Crypto/SelfTest/Hash/__init__.py @@ -34,10 +34,14 @@ def get_tests(config={}): import test_MD5; tests += test_MD5.get_tests(config=config) import test_RIPEMD; tests += test_RIPEMD.get_tests(config=config) import test_SHA; tests += test_SHA.get_tests(config=config) - import test_SHA224; tests += test_SHA224.get_tests(config=config) import test_SHA256; tests += test_SHA256.get_tests(config=config) - import test_SHA384; tests += test_SHA384.get_tests(config=config) - import test_SHA512; tests += test_SHA512.get_tests(config=config) + try: + import test_SHA224; tests += test_SHA224.get_tests(config=config) + import test_SHA384; tests += test_SHA384.get_tests(config=config) + import test_SHA512; tests += test_SHA512.get_tests(config=config) + except ImportError: + import sys + sys.stderr.write("SelfTest: warning: not testing SHA224/SHA384/SHA512 modules (not available)\n") return tests if __name__ == '__main__': diff --git a/lib/Crypto/SelfTest/Hash/test_HMAC.py b/lib/Crypto/SelfTest/Hash/test_HMAC.py index 9b25741..c01c97b 100644 --- a/lib/Crypto/SelfTest/Hash/test_HMAC.py +++ b/lib/Crypto/SelfTest/Hash/test_HMAC.py @@ -175,7 +175,9 @@ test_data = [ bfdc63644f0713938a7f51535c3a35e2 '''), 'RFC 4231 #7 (HMAC-SHA256)'), +] +hashlib_test_data = [ # Test case 8 (SHA224) ('4a656665', '7768617420646f2079612077616e74' @@ -200,9 +202,17 @@ test_data = [ ] def get_tests(config={}): - from Crypto.Hash import HMAC, MD5, SHA as SHA1, SHA256, SHA224, SHA384, SHA512 + global test_data + from Crypto.Hash import HMAC, MD5, SHA as SHA1, SHA256 from common import make_mac_tests - hashmods = dict(MD5=MD5, SHA1=SHA1, SHA224=SHA224, SHA256=SHA256, SHA384=SHA384, SHA512=SHA512, default=None) + hashmods = dict(MD5=MD5, SHA1=SHA1, SHA256=SHA256, default=None) + try: + from Crypto.Hash import SHA224, SHA384, SHA512 + hashmods.update(dict(SHA224=SHA224, SHA384=SHA384, SHA512=SHA512)) + test_data += hashlib_test_data + except ImportError: + import sys + sys.stderr.write("SelfTest: warning: not testing HMAC-SHA224/384/512 (not available)\n") return make_mac_tests(HMAC, "HMAC", test_data, hashmods) if __name__ == '__main__': @@ -84,6 +84,14 @@ else: if sys.platform != "win32": # Avoid nt.py, as 2to3 can't fix it w/o winrandom EXCLUDE_PY += [('Crypto.Random.OSRNG','nt')] +# Exclude SHA224/384/512 if they're not present (Python < 2.5) +try: from hashlib import sha224 +except ImportError: EXCLUDE_PY += [('Crypto.Hash', 'SHA224')] +try: from hashlib import sha384 +except ImportError: EXCLUDE_PY += [('Crypto.Hash', 'SHA384')] +try: from hashlib import sha512 +except ImportError: EXCLUDE_PY += [('Crypto.Hash', 'SHA512')] + # Work around the print / print() issue with Python 2.x and 3.x. We only need # to print at one point of the code, which makes this easy |