Changeset View
Changeset View
Standalone View
Standalone View
swh/model/hashutil.py
Show First 20 Lines • Show All 177 Lines • ▼ Show 20 Lines | if lalgo[7:]: | ||||
digest_size, remainder = divmod(int(lalgo[7:]), 8) | digest_size, remainder = divmod(int(lalgo[7:]), 8) | ||||
except ValueError: | except ValueError: | ||||
raise ValueError("Unknown digest size for algo %s" % algo) from None | raise ValueError("Unknown digest size for algo %s" % algo) from None | ||||
if remainder: | if remainder: | ||||
raise ValueError( | raise ValueError( | ||||
"Digest size for algorithm %s must be a multiple of 8" % algo | "Digest size for algorithm %s must be a multiple of 8" % algo | ||||
) | ) | ||||
if lalgo in hashlib.algorithms_available: | |||||
# Handle the case where OpenSSL ships the given algorithm | |||||
# (e.g. Python 3.5 on Debian 9 stretch) | |||||
_blake2_hash_cache[algo] = lambda: hashlib.new(lalgo) | |||||
else: | |||||
# Try using the built-in implementation for Python 3.6+ | |||||
if blake_family in hashlib.algorithms_available: | |||||
blake2 = getattr(hashlib, blake_family) | blake2 = getattr(hashlib, blake_family) | ||||
else: | |||||
import pyblake2 | |||||
blake2 = getattr(pyblake2, blake_family) | |||||
_blake2_hash_cache[algo] = lambda: blake2(digest_size=digest_size) | _blake2_hash_cache[algo] = lambda: blake2(digest_size=digest_size) | ||||
return _blake2_hash_cache[algo]() | return _blake2_hash_cache[algo]() | ||||
def _new_hashlib_hash(algo): | def _new_hashlib_hash(algo): | ||||
"""Initialize a digest object from hashlib. | """Initialize a digest object from hashlib. | ||||
Handle the swh-specific names for the blake2-related algorithms | Handle the swh-specific names for the blake2-related algorithms | ||||
▲ Show 20 Lines • Show All 158 Lines • Show Last 20 Lines |