Fast inline implementation of HMAC. key: bytes or buffer, The key for the keyed hash object. msg: bytes or buffer, Input message. digest: A hash name suitable for hashlib.new() for best performance. *OR* A hashlib constructor returning a new hash object. *OR* A m
(key, msg, digest)
| 219 | |
| 220 | |
| 221 | def digest(key, msg, digest): |
| 222 | """Fast inline implementation of HMAC. |
| 223 | |
| 224 | key: bytes or buffer, The key for the keyed hash object. |
| 225 | msg: bytes or buffer, Input message. |
| 226 | digest: A hash name suitable for hashlib.new() for best performance. *OR* |
| 227 | A hashlib constructor returning a new hash object. *OR* |
| 228 | A module supporting PEP 247. |
| 229 | """ |
| 230 | if _hashopenssl and isinstance(digest, (str, _functype)): |
| 231 | try: |
| 232 | return _hashopenssl.hmac_digest(key, msg, digest) |
| 233 | except OverflowError: |
| 234 | # OpenSSL's HMAC limits the size of the key to INT_MAX. |
| 235 | # Instead of falling back to HACL* implementation which |
| 236 | # may still not be supported due to a too large key, we |
| 237 | # directly switch to the pure Python fallback instead |
| 238 | # even if we could have used streaming HMAC for small keys |
| 239 | # but large messages. |
| 240 | return _compute_digest_fallback(key, msg, digest) |
| 241 | except _hashopenssl.UnsupportedDigestmodError: |
| 242 | pass |
| 243 | |
| 244 | if _hmac and isinstance(digest, str): |
| 245 | try: |
| 246 | return _hmac.compute_digest(key, msg, digest) |
| 247 | except (OverflowError, _hmac.UnknownHashError): |
| 248 | # HACL* HMAC limits the size of the key to UINT32_MAX |
| 249 | # so we fallback to the pure Python implementation even |
| 250 | # if streaming HMAC may have been used for small keys |
| 251 | # and large messages. |
| 252 | pass |
| 253 | |
| 254 | return _compute_digest_fallback(key, msg, digest) |
| 255 | |
| 256 | |
| 257 | def _compute_digest_fallback(key, msg, digest): |