Let us walk on the 3-isogeny graph
Loading...
Searching...
No Matches
Hashes Class Reference
Inheritance diagram for Hashes:

Public Member Functions

None __init__ (self, Optional[Dict[str, List[str]]] hashes=None)
 
"Hashes" __and__ (self, "Hashes" other)
 
int digest_count (self)
 
bool is_hash_allowed (self, str hash_name, str hex_digest)
 
None check_against_chunks (self, Iterable[bytes] chunks)
 
None check_against_file (self, BinaryIO file)
 
None check_against_path (self, str path)
 
bool has_one_of (self, Dict[str, str] hashes)
 
bool __bool__ (self)
 
bool __eq__ (self, object other)
 
int __hash__ (self)
 

Protected Member Functions

"NoReturn" _raise (self, Dict[str, "_Hash"] gots)
 

Protected Attributes

 _allowed
 

Detailed Description

A wrapper that builds multiple hashes at once and checks them against
known-good values

Definition at line 25 of file hashes.py.

Constructor & Destructor Documentation

◆ __init__()

None __init__ (   self,
Optional[Dict[str, List[str]]]   hashes = None 
)
:param hashes: A dict of algorithm names pointing to lists of allowed
    hex digests

Reimplemented in MissingHashes.

Definition at line 31 of file hashes.py.

31 def __init__(self, hashes: Optional[Dict[str, List[str]]] = None) -> None:
32 """
33 :param hashes: A dict of algorithm names pointing to lists of allowed
34 hex digests
35 """
36 allowed = {}
37 if hashes is not None:
38 for alg, keys in hashes.items():
39 # Make sure values are always sorted (to ease equality checks)
40 allowed[alg] = sorted(keys)
41 self._allowed = allowed
42
for i

References i.

Referenced by Protocol.__init_subclass__().

Here is the caller graph for this function:

Member Function Documentation

◆ __and__()

"Hashes" __and__ (   self,
"Hashes"  other 
)

Definition at line 43 of file hashes.py.

43 def __and__(self, other: "Hashes") -> "Hashes":
44 if not isinstance(other, Hashes):
45 return NotImplemented
46
47 # If either of the Hashes object is entirely empty (i.e. no hash
48 # specified at all), all hashes from the other object are allowed.
49 if not other:
50 return self
51 if not self:
52 return other
53
54 # Otherwise only hashes that present in both objects are allowed.
55 new = {}
56 for alg, values in other._allowed.items():
57 if alg not in self._allowed:
58 continue
59 new[alg] = [v for v in values if v in self._allowed[alg]]
60 return Hashes(new)
61

References Hashes._allowed, and i.

◆ __bool__()

bool __bool__ (   self)
Return whether I know any known-good hashes.

Definition at line 115 of file hashes.py.

115 def __bool__(self) -> bool:
116 """Return whether I know any known-good hashes."""
117 return bool(self._allowed)
118

References Hashes._allowed.

◆ __eq__()

bool __eq__ (   self,
object  other 
)

Definition at line 119 of file hashes.py.

119 def __eq__(self, other: object) -> bool:
120 if not isinstance(other, Hashes):
121 return NotImplemented
122 return self._allowed == other._allowed
123

References Hashes._allowed, and i.

Referenced by Version.__ge__(), Version.__gt__(), Version.__le__(), Version.__ne__(), Matcher.__ne__(), Timestamp.__ne__(), and HTTPHeaderDict.__ne__().

Here is the caller graph for this function:

◆ __hash__()

int __hash__ (   self)

Definition at line 124 of file hashes.py.

124 def __hash__(self) -> int:
125 return hash(
126 ",".join(
127 sorted(
128 ":".join((alg, digest))
129 for alg, digest_list in self._allowed.items()
130 for digest in digest_list
131 )
132 )
133 )
134
135

References Hashes._allowed.

Referenced by Style.__eq__(), and Style.__ne__().

Here is the caller graph for this function:

◆ _raise()

"NoReturn" _raise (   self,
Dict[str, "_Hash"]  gots 
)
protected

Reimplemented in MissingHashes.

Definition at line 93 of file hashes.py.

93 def _raise(self, gots: Dict[str, "_Hash"]) -> "NoReturn":
94 raise HashMismatch(self._allowed, gots)
95

References Hashes._allowed.

Referenced by Hashes.check_against_chunks().

Here is the caller graph for this function:

◆ check_against_chunks()

None check_against_chunks (   self,
Iterable[bytes]  chunks 
)
Check good hashes against ones built from iterable of chunks of
data.

Raise HashMismatch if none match.

Definition at line 70 of file hashes.py.

70 def check_against_chunks(self, chunks: Iterable[bytes]) -> None:
71 """Check good hashes against ones built from iterable of chunks of
72 data.
73
74 Raise HashMismatch if none match.
75
76 """
77 gots = {}
78 for hash_name in self._allowed.keys():
79 try:
80 gots[hash_name] = hashlib.new(hash_name)
81 except (ValueError, TypeError):
82 raise InstallationError(f"Unknown hash name: {hash_name}")
83
84 for chunk in chunks:
85 for hash in gots.values():
86 hash.update(chunk)
87
88 for hash_name, got in gots.items():
89 if got.hexdigest() in self._allowed[hash_name]:
90 return
91 self._raise(gots)
92

References Hashes._allowed, Hashes._raise(), MissingHashes._raise(), and i.

Referenced by Hashes.check_against_file().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_against_file()

None check_against_file (   self,
BinaryIO  file 
)
Check good hashes against a file-like object

Raise HashMismatch if none match.

Definition at line 96 of file hashes.py.

96 def check_against_file(self, file: BinaryIO) -> None:
97 """Check good hashes against a file-like object
98
99 Raise HashMismatch if none match.
100
101 """
102 return self.check_against_chunks(read_chunks(file))
103

References Hashes.check_against_chunks().

Referenced by Hashes.check_against_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_against_path()

None check_against_path (   self,
str  path 
)

Definition at line 104 of file hashes.py.

104 def check_against_path(self, path: str) -> None:
105 with open(path, "rb") as file:
106 return self.check_against_file(file)
107

References Hashes.check_against_file().

Here is the call graph for this function:

◆ digest_count()

int digest_count (   self)

Definition at line 63 of file hashes.py.

63 def digest_count(self) -> int:
64 return sum(len(digests) for digests in self._allowed.values())
65

References Hashes._allowed, and i.

◆ has_one_of()

bool has_one_of (   self,
Dict[str, str]  hashes 
)
Return whether any of the given hashes are allowed.

Definition at line 108 of file hashes.py.

108 def has_one_of(self, hashes: Dict[str, str]) -> bool:
109 """Return whether any of the given hashes are allowed."""
110 for hash_name, hex_digest in hashes.items():
111 if self.is_hash_allowed(hash_name, hex_digest):
112 return True
113 return False
114

References i, LinkHash.is_hash_allowed(), Link.is_hash_allowed(), and Hashes.is_hash_allowed().

Here is the call graph for this function:

◆ is_hash_allowed()

bool is_hash_allowed (   self,
str  hash_name,
str  hex_digest 
)
Return whether the given hex digest is allowed.

Definition at line 66 of file hashes.py.

66 def is_hash_allowed(self, hash_name: str, hex_digest: str) -> bool:
67 """Return whether the given hex digest is allowed."""
68 return hex_digest in self._allowed.get(hash_name, [])
69

References Hashes._allowed.

Referenced by Hashes.has_one_of().

Here is the caller graph for this function:

Field Documentation

◆ _allowed


The documentation for this class was generated from the following file: