(vector_data)
| 112 | |
| 113 | |
| 114 | def 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 | |
| 153 | def load_pkcs1_vectors(vector_data): |
no outgoing calls