我们从Python开源项目中,提取了以下37个代码示例,用于说明如何使用hashlib.name()。
def test_large_update(self): aas = 'a' * 128 bees = 'b' * 127 cees = 'c' * 126 abcs = aas + bees + cees for name in self.supported_hash_names: m1 = hashlib.new(name) m1.update(aas) m1.update(bees) m1.update(cees) m2 = hashlib.new(name) m2.update(abcs) self.assertEqual(m1.digest(), m2.digest(), name+' update problem.') m3 = hashlib.new(name, abcs) self.assertEqual(m1.digest(), m3.digest(), name+' new problem.')
def check(self, name, data, hexdigest): hexdigest = hexdigest.lower() constructors = self.constructors_to_test[name] # 2 is for hashlib.name(...) and hashlib.new(name, ...) self.assertGreaterEqual(len(constructors), 2) for hash_object_constructor in constructors: m = hash_object_constructor(data) computed = m.hexdigest() self.assertEqual( computed, hexdigest, "Hash algorithm %s constructed using %s returned hexdigest" " %r for %d byte input data that should have hashed to %r." % (name, hash_object_constructor, computed, len(data), hexdigest)) computed = m.digest() digest = bytes.fromhex(hexdigest) self.assertEqual(computed, digest) self.assertEqual(len(digest), m.digest_size)
def test_unknown_hash(self): try: hashlib.new('spam spam spam spam spam') except ValueError: pass else: self.assertTrue(0 == "hashlib didn't reject bogus hash name")
def test_hexdigest(self): for name in self.supported_hash_names: h = hashlib.new(name) assert isinstance(h.digest(), bytes), name self.assertEqual(hexstr(h.digest()), h.hexdigest())
def test_large_update(self): aas = b'a' * 128 bees = b'b' * 127 cees = b'c' * 126 for name in self.supported_hash_names: m1 = hashlib.new(name) m1.update(aas) m1.update(bees) m1.update(cees) m2 = hashlib.new(name) m2.update(aas + bees + cees) self.assertEqual(m1.digest(), m2.digest())
def check(self, name, data, digest): constructors = self.constructors_to_test[name] # 2 is for hashlib.name(...) and hashlib.new(name, ...) self.assertGreaterEqual(len(constructors), 2) for hash_object_constructor in constructors: computed = hash_object_constructor(data).hexdigest() self.assertEqual( computed, digest, "Hash algorithm %s constructed using %s returned hexdigest" " %r for %d byte input data that should have hashed to %r." % (name, hash_object_constructor, computed, len(data), digest))
def test_hexdigest(self): for name in self.supported_hash_names: h = hashlib.new(name) self.assertTrue(hexstr(h.digest()) == h.hexdigest())
def check_update(self, name, data, digest): constructors = self.constructors_to_test[name] # 2 is for hashlib.name(...) and hashlib.new(name, ...) self.assertGreaterEqual(len(constructors), 2) for hash_object_constructor in constructors: h = hash_object_constructor() h.update(data) computed = h.hexdigest() self.assertEqual( computed, digest, "Hash algorithm %s using %s when updated returned hexdigest" " %r for %d byte input data that should have hashed to %r." % (name, hash_object_constructor, computed, len(data), digest))
def test_hexdigest(self): for cons in self.hash_constructors: h = cons() assert isinstance(h.digest(), bytes), name self.assertEqual(hexstr(h.digest()), h.hexdigest())
def check_blocksize_name(self, name, block_size=0, digest_size=0): constructors = self.constructors_to_test[name] for hash_object_constructor in constructors: m = hash_object_constructor() self.assertEqual(m.block_size, block_size) self.assertEqual(m.digest_size, digest_size) self.assertEqual(len(m.digest()), digest_size) self.assertEqual(m.name.lower(), name.lower()) self.assertIn(name.split("_")[0], repr(m).lower())
def test_name_attribute(self): for cons in self.hash_constructors: h = cons() self.assertIsInstance(h.name, str) self.assertIn(h.name, self.supported_hash_names) self.assertEqual(h.name, hashlib.new(h.name).name)
def check_blocksize_name(self, name, block_size=0, digest_size=0): constructors = self.constructors_to_test[name] for hash_object_constructor in constructors: m = hash_object_constructor() self.assertEqual(m.block_size, block_size) self.assertEqual(m.digest_size, digest_size) self.assertEqual(len(m.digest()), digest_size) self.assertEqual(m.name, name) # split for sha3_512 / _sha3.sha3 object self.assertIn(name.split("_")[0], repr(m))
def __init__(self, *args, **kwargs): algorithms = set() for algorithm in self.supported_hash_names: algorithms.add(algorithm.lower()) self.constructors_to_test = {} for algorithm in algorithms: self.constructors_to_test[algorithm] = set() # For each algorithm, test the direct constructor and the use # of hashlib.new given the algorithm name. for algorithm, constructors in self.constructors_to_test.items(): constructors.add(getattr(hashlib, algorithm)) def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm): if data is None: return hashlib.new(_alg) return hashlib.new(_alg, data) constructors.add(_test_algorithm_via_hashlib_new) _hashlib = self._conditional_import_module('_hashlib') if _hashlib: # These two algorithms should always be present when this module # is compiled. If not, something was compiled wrong. assert hasattr(_hashlib, 'openssl_md5') assert hasattr(_hashlib, 'openssl_sha1') for algorithm, constructors in self.constructors_to_test.items(): constructor = getattr(_hashlib, 'openssl_'+algorithm, None) if constructor: constructors.add(constructor) _md5 = self._conditional_import_module('_md5') if _md5: self.constructors_to_test['md5'].add(_md5.md5) _sha1 = self._conditional_import_module('_sha1') if _sha1: self.constructors_to_test['sha1'].add(_sha1.sha1) _sha256 = self._conditional_import_module('_sha256') if _sha256: self.constructors_to_test['sha224'].add(_sha256.sha224) self.constructors_to_test['sha256'].add(_sha256.sha256) _sha512 = self._conditional_import_module('_sha512') if _sha512: self.constructors_to_test['sha384'].add(_sha512.sha384) self.constructors_to_test['sha512'].add(_sha512.sha512) super(HashLibTestCase, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): algorithms = set() for algorithm in self.supported_hash_names: algorithms.add(algorithm.lower()) self.constructors_to_test = {} for algorithm in algorithms: self.constructors_to_test[algorithm] = set() # For each algorithm, test the direct constructor and the use # of hashlib.new given the algorithm name. for algorithm, constructors in self.constructors_to_test.items(): constructors.add(getattr(hashlib, algorithm)) def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm): if data is None: return hashlib.new(_alg) return hashlib.new(_alg, data) constructors.add(_test_algorithm_via_hashlib_new) _hashlib = self._conditional_import_module('_hashlib') if _hashlib: # These two algorithms should always be present when this module # is compiled. If not, something was compiled wrong. assert hasattr(_hashlib, 'openssl_md5') assert hasattr(_hashlib, 'openssl_sha1') for algorithm, constructors in self.constructors_to_test.items(): constructor = getattr(_hashlib, 'openssl_'+algorithm, None) if constructor: constructors.add(constructor) _md5 = self._conditional_import_module('_md5') if _md5: self.constructors_to_test['md5'].add(_md5.new) _sha = self._conditional_import_module('_sha') if _sha: self.constructors_to_test['sha1'].add(_sha.new) _sha256 = self._conditional_import_module('_sha256') if _sha256: self.constructors_to_test['sha224'].add(_sha256.sha224) self.constructors_to_test['sha256'].add(_sha256.sha256) _sha512 = self._conditional_import_module('_sha512') if _sha512: self.constructors_to_test['sha384'].add(_sha512.sha384) self.constructors_to_test['sha512'].add(_sha512.sha512) super(HashLibTestCase, self).__init__(*args, **kwargs)
def __init__(self, *args, **kwargs): algorithms = set() for algorithm in self.supported_hash_names: algorithms.add(algorithm.lower()) self.constructors_to_test = {} for algorithm in algorithms: self.constructors_to_test[algorithm] = set() # For each algorithm, test the direct constructor and the use # of hashlib.new given the algorithm name. for algorithm, constructors in self.constructors_to_test.items(): constructors.add(getattr(hashlib, algorithm)) def _test_algorithm_via_hashlib_new(data=None, _alg=algorithm): if data is None: return hashlib.new(_alg) return hashlib.new(_alg, data) constructors.add(_test_algorithm_via_hashlib_new) _hashlib = self._conditional_import_module('_hashlib') if _hashlib: # These two algorithms should always be present when this module # is compiled. If not, something was compiled wrong. self.assertTrue(hasattr(_hashlib, 'openssl_md5')) self.assertTrue(hasattr(_hashlib, 'openssl_sha1')) for algorithm, constructors in self.constructors_to_test.items(): constructor = getattr(_hashlib, 'openssl_'+algorithm, None) if constructor: constructors.add(constructor) def add_builtin_constructor(name): constructor = getattr(hashlib, "__get_builtin_constructor")(name) self.constructors_to_test[name].add(constructor) _md5 = self._conditional_import_module('_md5') if _md5: add_builtin_constructor('md5') _sha1 = self._conditional_import_module('_sha1') if _sha1: add_builtin_constructor('sha1') _sha256 = self._conditional_import_module('_sha256') if _sha256: add_builtin_constructor('sha224') add_builtin_constructor('sha256') _sha512 = self._conditional_import_module('_sha512') if _sha512: add_builtin_constructor('sha384') add_builtin_constructor('sha512') super(HashLibTestCase, self).__init__(*args, **kwargs)