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)
| 231 | |
| 232 | |
| 233 | def digest(key, msg, digest): |
| 234 | """Fast inline implementation of HMAC. |
| 235 | |
| 236 | key: bytes or buffer, The key for the keyed hash object. |
| 237 | msg: bytes or buffer, Input message. |
| 238 | digest: A hash name suitable for hashlib.new() for best performance. *OR* |
| 239 | A hashlib constructor returning a new hash object. *OR* |
| 240 | A module supporting PEP 247. |
| 241 | """ |
| 242 | if _hashopenssl and isinstance(digest, (str, _functype)): |
| 243 | try: |
| 244 | return _hashopenssl.hmac_digest(key, msg, digest) |
| 245 | except OverflowError: |
| 246 | # OpenSSL's HMAC limits the size of the key to INT_MAX. |
| 247 | # Instead of falling back to HACL* implementation which |
| 248 | # may still not be supported due to a too large key, we |
| 249 | # directly switch to the pure Python fallback instead |
| 250 | # even if we could have used streaming HMAC for small keys |
| 251 | # but large messages. |
| 252 | return _compute_digest_fallback(key, msg, digest) |
| 253 | except _hashopenssl.UnsupportedDigestmodError: |
| 254 | pass |
| 255 | |
| 256 | if _hmac and isinstance(digest, str): |
| 257 | try: |
| 258 | return _hmac.compute_digest(key, msg, digest) |
| 259 | except (OverflowError, _hmac.UnknownHashError): |
| 260 | # HACL* HMAC limits the size of the key to UINT32_MAX |
| 261 | # so we fallback to the pure Python implementation even |
| 262 | # if streaming HMAC may have been used for small keys |
| 263 | # and large messages. |
| 264 | pass |
| 265 | |
| 266 | return _compute_digest_fallback(key, msg, digest) |
| 267 | |
| 268 | |
| 269 | def _compute_digest_fallback(key, msg, digest): |
searching dependent graphs…