def hashex(method, # type: HashMethod
key, # type: KeyType
**options # type: typing.Any
):
# type: (...) -> int
if isinstance(key, six.text_type):
key = key.encode('utf-8')
if method.name.lower() in hashlib.algorithms_guaranteed:
return int(hashlib.new(method.name.lower(), key).hexdigest(), 16)
elif method == HashMethod.MMH3_32:
return hash_murmur3(key, size=32, **options)
elif method == HashMethod.MMH3_64:
return hash_murmur3(key, size=64, **options)
elif method == HashMethod.MMH3_128:
return hash_murmur3(key, size=128, **options)
elif method == HashMethod.SIPHASH:
return hash_siphash(key, **options)
python类algorithms_guaranteed()的实例源码
def test_hashex_hashlib():
for algorithm in hashlib.algorithms_guaranteed:
method = getattr(proxenos.rendezvous.HashMethod, algorithm.upper())
assert (proxenos.rendezvous.hashex(method, 'secret') ==
int(hashlib.new(algorithm, b'secret').hexdigest(), 16))
def test_basic_statements(self):
for algorithm in hashlib.algorithms_guaranteed:
if algorithm.startswith("blake2") or algorithm.startswith("sha3") or algorithm.startswith("shake"):
with self.assertRaises(Exception):
rehash.ResumableHasher(algorithm.lower())
else:
print(algorithm)
self.assert_resumable(rehash.new(algorithm.lower()))
self.assert_resumable(rehash.new(algorithm.lower(), b"initial_data"))
self.assert_resumable(rehash.ResumableHasher(algorithm.lower()))
self.assert_resumable(rehash.ResumableHasher(algorithm.lower(), b"initial_data"))
self.assert_resumable(getattr(rehash, algorithm)())
self.assert_resumable(getattr(rehash, algorithm)(b"initial_data"))
def test_algorithms_guaranteed(self):
self.assertEqual(hashlib.algorithms_guaranteed,
set(_algo for _algo in self.supported_hash_names
if _algo.islower()))
def test_algorithms_available(self):
self.assertTrue(set(hashlib.algorithms_guaranteed).
issubset(hashlib.algorithms_available))
def test_algorithms_guaranteed(self):
self.assertEqual(hashlib.algorithms_guaranteed,
set(_algo for _algo in self.supported_hash_names
if _algo.islower()))
def test_algorithms_available(self):
self.assertTrue(set(hashlib.algorithms_guaranteed).
issubset(hashlib.algorithms_available))
def test_algorithms_guaranteed(self):
self.assertEqual(hashlib.algorithms_guaranteed,
set(_algo for _algo in self.supported_hash_names
if _algo.islower()))
def test_algorithms_available(self):
self.assertTrue(set(hashlib.algorithms_guaranteed).
issubset(hashlib.algorithms_available))
def _get_hash_object(hash_algo_name):
"""Create a hash object based on given algorithm.
:param hash_algo_name: name of the hashing algorithm.
:raises: InvalidInputError, on unsupported or invalid input.
:returns: a hash object based on the given named algorithm.
"""
algorithms = (hashlib.algorithms_guaranteed if six.PY3
else hashlib.algorithms)
if hash_algo_name not in algorithms:
msg = ("Unsupported/Invalid hash name '%s' provided."
% hash_algo_name)
raise exception.InvalidInputError(msg)
return getattr(hashlib, hash_algo_name)()
def test_algorithms_guaranteed(self):
self.assertEqual(hashlib.algorithms_guaranteed,
set(_algo for _algo in self.supported_hash_names
if _algo.islower()))
def test_algorithms_available(self):
self.assertTrue(set(hashlib.algorithms_guaranteed).
issubset(hashlib.algorithms_available))
def hash(self, mask, target, args):
"""Hash text.
%%hash (--md5 | --sha1 | --sha256 | --sha512) <string>...
"""
available_algorithms = hashlib.algorithms_guaranteed
text = ' '.join(args['<string>']).encode('UTF-8')
for algo in available_algorithms:
flag = '--{0}'.format(algo)
if flag in args and args[flag]:
hash_object = hashlib.new(algo)
hash_object.update(text)
return '{0}: {1}'.format(mask.nick, hash_object.hexdigest())
def test_algorithms_guaranteed(self):
self.assertEqual(hashlib.algorithms_guaranteed,
set(_algo for _algo in self.supported_hash_names
if _algo.islower()))
def test_algorithms_available(self):
self.assertTrue(set(hashlib.algorithms_guaranteed).
issubset(hashlib.algorithms_available))
def test_algorithms_guaranteed(self):
self.assertEqual(hashlib.algorithms_guaranteed,
set(_algo for _algo in self.supported_hash_names
if _algo.islower()))
def test_algorithms_available(self):
self.assertTrue(set(hashlib.algorithms_guaranteed).
issubset(hashlib.algorithms_available))
def test_algorithms_guaranteed(self):
self.assertEqual(hashlib.algorithms_guaranteed,
set(_algo for _algo in self.supported_hash_names
if _algo.islower()))
def test_algorithms_available(self):
self.assertTrue(set(hashlib.algorithms_guaranteed).
issubset(hashlib.algorithms_available))