Title: Unify recognization of message digest names for `_hashlib` and `_hmac` · Issue #131876 · python/cpython · GitHub
Open Graph Title: Unify recognization of message digest names for `_hashlib` and `_hmac` · Issue #131876 · python/cpython
X Title: Unify recognization of message digest names for `_hashlib` and `_hmac` · Issue #131876 · python/cpython
Description: Feature or enhancement When calling hmac.new(key, digestmod=HASH), the HASH can be: a named algorithm recognized by hashlib.new, (e.g., sha256); a digest constructor (e.g., hashlib.openssl_sha256); an object supporting PEP-247. In #13015...
Open Graph Description: Feature or enhancement When calling hmac.new(key, digestmod=HASH), the HASH can be: a named algorithm recognized by hashlib.new, (e.g., sha256); a digest constructor (e.g., hashlib.openssl_sha256);...
X Description: Feature or enhancement When calling hmac.new(key, digestmod=HASH), the HASH can be: a named algorithm recognized by hashlib.new, (e.g., sha256); a digest constructor (e.g., hashlib.openssl_sha256);...
Opengraph URL: https://github.com/python/cpython/issues/131876
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Unify recognization of message digest names for `_hashlib` and `_hmac`","articleBody":"# Feature or enhancement\n\nWhen calling `hmac.new(key, digestmod=HASH)`, the `HASH` can be:\n\n- a named algorithm recognized by `hashlib.new`, (e.g., `sha256`); \n- a digest constructor (e.g., `hashlib.openssl_sha256`);\n- an object supporting PEP-247.\n\nIn #130157, I've only supported named algorithms as HACL* only supports named algorithms. When using OpenSSL HMAC instead of HACL* HMAC, determining which hash function to use from HASH is left to the `hashlib` C implementation, which itself delegates this task to OpenSSL based on NIDs.\n\nI'm creating this issue so that we can brainstorm and decide how we should make HACL* HMAC able to also possibly detect objects supporting PEP-247. I plan to first upgrade the HMAC documentation:\n\n\u003e Return a new hmac object. *key* is a bytes or bytearray object giving the secret key. If *msg* is present, the method call *update(msg)* is made. *digestmod* is the digest name, digest constructor or module for the HMAC object to use. It may be any name suitable to [hashlib.new()](https://docs.python.org/3/library/hashlib.html#hashlib.new). Despite its argument position, it is required.\n\nAs you may see, the terms \"digest constructor\" and \"module\" are not well-defined. So I first plan to explain these two. In a second phase, I plan to extract the code in `_hashopenssl.c` responsible for determining whether a name is known or not in a separate module so that it can be shared with the HMAC C implementation later. The idea is to ease the future transition where we would drop OpenSSL (but this is still not planned nor decided, and this would likely require a PEP) and entirely rely on HACL* instead, both for speed and security.\n\nSome questions we need to address before letting HACL* HMAC support non-named algorithms:\n\n- Should we restrict digest constructors to HACL* ones only or not?\n- If not, should we consider `hashlib.openssl_sha256` equivalent to using HACL* SHA-256?\n- If not again, should we fallback to a generic implementation of HMAC which directly calls that callable\n\nCurrently, passing a digest constructor to the (OpenSSL) C implementation does not mean that we're using it. We're actually using it to recover the algorithm name, so it can regarded as an alias.\n\nOTOH, in the **Python** implementation of HMAC, any callable is considered a digest constructor and will be used as is. In some sense, it's a way to have HMAC implemented using an arbitrary hash function (HMAC is designed as such).\n\n\u003c!-- gh-linked-prs --\u003e\n### Linked PRs\n* gh-136995\n* gh-137301\n* gh-137307\n* gh-137319\n\u003c!-- /gh-linked-prs --\u003e\n","author":{"url":"https://github.com/picnixz","@type":"Person","name":"picnixz"},"datePublished":"2025-03-29T10:48:28.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":1},"url":"https://github.com/131876/cpython/issues/131876"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:81db4948-f27c-09f0-5fd8-782df5689721 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 8B14:332828:B2574:F7069:696A0B29 |
| html-safe-nonce | 44caae9fa4689d0665e9a027073098aae08621f326e5132b12772563e2ce9189 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4QjE0OjMzMjgyODpCMjU3NDpGNzA2OTo2OTZBMEIyOSIsInZpc2l0b3JfaWQiOiI0NjE3NTMyNzE0OTYzNDY0MDkiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b2816604386422dc1122031fa32f7d605991779ef5cf31ef89e37d959c117e92 |
| hovercard-subject-tag | issue:2957934243 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/131876/issue_layout |
| twitter:image | https://opengraph.githubassets.com/693142453c69345d7b65084c1d6073bef1ff10b46079c0fa3cd7c968e1333448/python/cpython/issues/131876 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/693142453c69345d7b65084c1d6073bef1ff10b46079c0fa3cd7c968e1333448/python/cpython/issues/131876 |
| og:image:alt | Feature or enhancement When calling hmac.new(key, digestmod=HASH), the HASH can be: a named algorithm recognized by hashlib.new, (e.g., sha256); a digest constructor (e.g., hashlib.openssl_sha256);... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | picnixz |
| hostname | github.com |
| expected-hostname | github.com |
| None | 699227a00bbb7fe1eec276d2ae1c3a93068bc5ba483bd9dc4b2a27a8f4f2f595 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 7266b2d935baa1c6474b16dd9feaa5ca30607261 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width