MCPcopy Create free account
hub / github.com/pyca/cryptography / load_hash_vectors

Function load_hash_vectors

tests/utils.py:114–150  ·  view source on GitHub ↗
(vector_data)

Source from the content-addressed store, hash-verified

112
113
114def load_hash_vectors(vector_data):
115 vectors: typing.List[typing.Union[KeyedHashVector, HashVector]] = []
116 key = None
117 msg = None
118 md = None
119
120 for line in vector_data:
121 line = line.strip()
122
123 if not line or line.startswith("#") or line.startswith("["):
124 continue
125
126 if line.startswith("Len"):
127 length = int(line.split(" = ")[1])
128 elif line.startswith("Key"):
129 # HMAC vectors contain a key attribute. Hash vectors do not.
130 key = line.split(" = ")[1].encode("ascii")
131 elif line.startswith("Msg"):
132 # In the NIST vectors they have chosen to represent an empty
133 # string as hex 00, which is of course not actually an empty
134 # string. So we parse the provided length and catch this edge case.
135 msg = line.split(" = ")[1].encode("ascii") if length > 0 else b""
136 elif line.startswith("MD") or line.startswith("Output"):
137 md = line.split(" = ")[1]
138 # after MD is found the Msg+MD (+ potential key) tuple is complete
139 if key is not None:
140 vectors.append(KeyedHashVector(msg, md, key))
141 key = None
142 msg = None
143 md = None
144 else:
145 vectors.append(HashVector(msg, md))
146 msg = None
147 md = None
148 else:
149 raise ValueError("Unknown line in hash vector")
150 return vectors
151
152
153def load_pkcs1_vectors(vector_data):

Callers 3

test_load_hash_vectorsFunction · 0.85
test_load_hmac_vectorsFunction · 0.85

Calls

no outgoing calls

Tested by 3

test_load_hash_vectorsFunction · 0.68
test_load_hmac_vectorsFunction · 0.68