diff options
author | Dwayne Litzenberger <dlitz@dlitz.net> | 2013-02-17 11:21:35 -0800 |
---|---|---|
committer | Dwayne Litzenberger <dlitz@dlitz.net> | 2013-02-17 19:18:29 -0800 |
commit | 0d8ea5ff1607a3d7ae544667bff99229954484ff (patch) | |
tree | d4d702f991fcb945675b325d7ff4e103c64d392f /lib/Crypto/SelfTest | |
parent | 59018ff99c97261f9bbaee33f919938871e05118 (diff) | |
download | pycrypto-0d8ea5ff1607a3d7ae544667bff99229954484ff.tar.gz |
Hash: Generic Crypto.Hash.new(algo, [data]) function
This allows us to instantiate a new hash given only an existing hash
object.
Diffstat (limited to 'lib/Crypto/SelfTest')
-rw-r--r-- | lib/Crypto/SelfTest/Hash/common.py | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/Crypto/SelfTest/Hash/common.py b/lib/Crypto/SelfTest/Hash/common.py index fa9fd22..487e2cb 100644 --- a/lib/Crypto/SelfTest/Hash/common.py +++ b/lib/Crypto/SelfTest/Hash/common.py @@ -29,6 +29,7 @@ __revision__ = "$Id$" import sys import unittest import binascii +import Crypto.Hash from Crypto.Util.py3compat import * # For compatibility with Python 2.1 and Python 2.2 @@ -100,6 +101,19 @@ class HashSelfTest(unittest.TestCase): out5 = binascii.b2a_hex(h2.digest()) self.assertEqual(self.expected, out5) + # Verify that Crypto.Hash.new(h) produces a fresh hash object + h3 = Crypto.Hash.new(h) + h3.update(self.input) + out6 = binascii.b2a_hex(h3.digest()) + self.assertEqual(self.expected, out6) + + if hasattr(h, 'name'): + # Verify that Crypto.Hash.new(h.name) produces a fresh hash object + h4 = Crypto.Hash.new(h.name) + h4.update(self.input) + out7 = binascii.b2a_hex(h4.digest()) + self.assertEqual(self.expected, out7) + class HashTestOID(unittest.TestCase): def __init__(self, hashmod, oid): unittest.TestCase.__init__(self) @@ -111,6 +125,25 @@ class HashTestOID(unittest.TestCase): h = self.hashmod.new() self.assertEqual(PKCS1_v1_5._HASH_OIDS[h.name], self.oid) +class GenericHashConstructorTest(unittest.TestCase): + def __init__(self, hashmod): + unittest.TestCase.__init__(self) + self.hashmod = hashmod + + def runTest(self): + obj1 = self.hashmod.new("foo") + obj2 = self.hashmod.new() + obj3 = Crypto.Hash.new(obj1.name, "foo") + obj4 = Crypto.Hash.new(obj1.name) + obj5 = Crypto.Hash.new(obj1, "foo") + obj6 = Crypto.Hash.new(obj1) + self.assert_(isinstance(self.hashmod, obj1)) + self.assert_(isinstance(self.hashmod, obj2)) + self.assert_(isinstance(self.hashmod, obj3)) + self.assert_(isinstance(self.hashmod, obj4)) + self.assert_(isinstance(self.hashmod, obj5)) + self.assert_(isinstance(self.hashmod, obj6)) + class MACSelfTest(unittest.TestCase): def __init__(self, hashmod, description, expected_dict, input, key, hashmods): @@ -175,6 +208,8 @@ def make_hash_tests(module, module_name, test_data, digest_size, oid=None): tests.append(HashDigestSizeSelfTest(module, name, digest_size)) if oid is not None: tests.append(HashTestOID(module, b(oid))) + if getattr(module, 'name', None) is not None: + tests.append(GenericHashConstructorTest(module)) return tests def make_mac_tests(module, module_name, test_data, hashmods): |