2from typing
import TYPE_CHECKING, BinaryIO, Dict, Iterable, List, Optional
8 from hashlib
import _Hash
12 from typing
import NoReturn
17FAVORITE_HASH =
"sha256"
22STRONG_HASHES = [
"sha256",
"sha384",
"sha512"]
26 """A wrapper that builds multiple hashes at once and checks them against
31 def __init__(self, hashes: Optional[Dict[str, List[str]]] =
None) ->
None:
33 :param hashes: A dict of algorithm names pointing to lists of allowed
37 if hashes
is not None:
40 allowed[alg] = sorted(keys)
43 def __and__(self, other:
"Hashes") ->
"Hashes":
59 new[alg] = [v
for v
in values
if v
in self.
_allowed[alg]]
67 """Return whether the given hex digest is allowed."""
68 return hex_digest
in self.
_allowed.get(hash_name, [])
71 """Check good hashes against ones built from iterable of chunks of
74 Raise HashMismatch if none match.
78 for hash_name
in self.
_allowed.keys():
81 except (ValueError, TypeError):
93 def _raise(self, gots: Dict[str,
"_Hash"]) ->
"NoReturn":
97 """Check good hashes against a file-like object
99 Raise HashMismatch if none match.
105 with open(path,
"rb")
as file:
109 """Return whether any of the given hashes are allowed."""
116 """Return whether I know any known-good hashes."""
121 return NotImplemented
128 ":".join((alg, digest))
129 for alg, digest_list
in self.
_allowed.items()
130 for digest
in digest_list
137 """A workalike for Hashes used when we're missing a hash for a requirement
139 It computes the actual hash of the requirement and raises a HashMissing
140 exception showing it to the user.
145 """Don't offer the ``hashes`` kwarg."""
150 def _raise(self, gots: Dict[str,
"_Hash"]) ->
"NoReturn":
None __init__(self, Optional[Dict[str, List[str]]] hashes=None)
None check_against_chunks(self, Iterable[bytes] chunks)
None check_against_file(self, BinaryIO file)
"Hashes" __and__(self, "Hashes" other)
bool __eq__(self, object other)
bool is_hash_allowed(self, str hash_name, str hex_digest)
None check_against_path(self, str path)
"NoReturn" _raise(self, Dict[str, "_Hash"] gots)
bool has_one_of(self, Dict[str, str] hashes)
"NoReturn" _raise(self, Dict[str, "_Hash"] gots)