Repository navigation
Simplify hashlib implementation #82313
Description
Activity
The hashlib module uses complicated macros and some pointless optimization. The code can be simplified to make maintenance easier. Cleanup also makes it easier to get rid of static global state and to add "usedforsecurity" feature.
-
The EVPobject contains a PyObject* with the name of the hashing algorithm as PyUnicode object. The name is rarely used and can be efficiently calculated from a const char* of the EVP_MD_CTX.
-
The module caches pre-generated EVP_MD_CTX objects for commonly used hashes like sha256. Tests with timeit has shown that generating a EVP_MD_CTX from a EVP constructor is as fast as copying and reinitializing a EVP_MD_CTX.
-
The pre-calculated constructs can be replaced with argument clinic to make the code more readable.
-
- addedextension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancement
on Sep 12, 2019 Yes that would be awesome!
Indeed hashlib has been a bit of a pain to work with especially with FIPS related modifications, simplifying it would help a ton.
AFAICS, all three suggested items was implemented by gh-16023:
- The
EVPobjectname is now fetched from theEVP_MD_CTXon demand - The pre-generated static objects were removed from the extension module
- The extension module was adapted to use Argument Clinic
Closing as completed.
- The
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields: