Skip to content

gh-131876: extract _hashlib helpers into a separate directory [WIP] #135341

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Prev Previous commit
Next Next commit
post-merge
  • Loading branch information
picnixz committed Jul 22, 2025
commit 7a45a9be467159efff5aba67574305be478881cd
73 changes: 41 additions & 32 deletions Modules/_hashlib/hashlib_fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,56 +50,65 @@

#include "Python.h"

#define Py_HASHLIB_MD_NS(ATTR) Py_hashlib_message_digest_ ## ATTR
#define Py_HASHLIB_MD_FAMILY(FAMILY_ID) Py_HASHLIB_MD_NS(family_ ## FAMILY_ID)
#define Py_HASHLIB_MD_MEMBER(MEMBER_ID) Py_HASHLIB_MD_NS(member_ ## MEMBER_ID)
/*
* Internal error messages used for reporting an unsupported hash algorithm.
* The algorithm can be given by its name, a callable or a PEP-247 module.
* The same message is raised by Lib/hashlib.py::__get_builtin_constructor()
* and _hmacmodule.c::find_hash_info().
*/
#define _Py_HASHLIB_UNSUPPORTED_ALGORITHM "unsupported hash algorithm %S"
#define _Py_HASHLIB_UNSUPPORTED_STR_ALGORITHM "unsupported hash algorithm %s"

#define _Py_HASHLIB_MD_NS(ATTR) _Py_hashlib_message_digest_ ## ATTR
#define _Py_HASHLIB_MD_FAMILY(FAMILY) _Py_HASHLIB_MD_NS(family_ ## FAMILY)
#define _Py_HASHLIB_MD_MEMBER(MEMBER) _Py_HASHLIB_MD_NS(member_ ## MEMBER)

#define Py_HASHLIB_MD_NAMES Py_HASHLIB_MD_NS(NAMES)
#define Py_HASHLIB_MD_COUNT Py_ARRAY_LENGTH(Py_HASHLIB_MD_NAMES)
#define Py_HASHLIB_MD_NAME(MEMBER_ID) \
#define _Py_HASHLIB_MD_NAMES _Py_HASHLIB_MD_NS(NAMES)
#define _Py_HASHLIB_MD_COUNT Py_ARRAY_LENGTH(Py_HASHLIB_MD_NAMES)
#define _Py_HASHLIB_MD_NAME(MEMBER_ID) \
( \
assert(Py_HASHLIB_MD_NAME(MEMBER_ID) < Py_HASHLIB_MD_COUNT), \
Py_HASHLIB_MD_NAMES[Py_HASHLIB_MD_MEMBER(MEMBER_ID)] \
)

typedef enum {
Py_HASHLIB_MD_FAMILY(MD) = 0,
Py_HASHLIB_MD_FAMILY(SHA1),
Py_HASHLIB_MD_FAMILY(SHA2),
Py_HASHLIB_MD_FAMILY(SHA2t),
Py_HASHLIB_MD_FAMILY(SHA3),
Py_HASHLIB_MD_FAMILY(SHA3_XOF),
Py_HASHLIB_MD_FAMILY(BLAKE2),
} Py_HASHLIB_MD_NS(family);
_Py_HASHLIB_MD_FAMILY(MD) = 0,
_Py_HASHLIB_MD_FAMILY(SHA1),
_Py_HASHLIB_MD_FAMILY(SHA2),
_Py_HASHLIB_MD_FAMILY(SHA2t),
_Py_HASHLIB_MD_FAMILY(SHA3),
_Py_HASHLIB_MD_FAMILY(SHA3_XOF),
_Py_HASHLIB_MD_FAMILY(BLAKE2),
} _Py_HASHLIB_MD_NS(family);

typedef enum {
/* MD-family */
Py_HASHLIB_MD_MEMBER(md5) = 0,
_Py_HASHLIB_MD_MEMBER(md5) = 0,
/* SHA-1 family */
Py_HASHLIB_MD_MEMBER(sha1),
_Py_HASHLIB_MD_MEMBER(sha1),
/* SHA-2 family */
Py_HASHLIB_MD_MEMBER(sha224),
Py_HASHLIB_MD_MEMBER(sha256),
Py_HASHLIB_MD_MEMBER(sha384),
Py_HASHLIB_MD_MEMBER(sha512),
_Py_HASHLIB_MD_MEMBER(sha224),
_Py_HASHLIB_MD_MEMBER(sha256),
_Py_HASHLIB_MD_MEMBER(sha384),
_Py_HASHLIB_MD_MEMBER(sha512),
/* Truncated SHA-2 family */
Py_HASHLIB_MD_MEMBER(sha512_224),
Py_HASHLIB_MD_MEMBER(sha512_256),
_Py_HASHLIB_MD_MEMBER(sha512_224),
_Py_HASHLIB_MD_MEMBER(sha512_256),
/* SHA-3 family */
Py_HASHLIB_MD_MEMBER(sha3_224),
Py_HASHLIB_MD_MEMBER(sha3_256),
Py_HASHLIB_MD_MEMBER(sha3_384),
Py_HASHLIB_MD_MEMBER(sha3_512),
_Py_HASHLIB_MD_MEMBER(sha3_224),
_Py_HASHLIB_MD_MEMBER(sha3_256),
_Py_HASHLIB_MD_MEMBER(sha3_384),
_Py_HASHLIB_MD_MEMBER(sha3_512),
/* SHA-3 XOF SHAKE family */
Py_HASHLIB_MD_MEMBER(shake_128),
Py_HASHLIB_MD_MEMBER(shake_256),
_Py_HASHLIB_MD_MEMBER(shake_128),
_Py_HASHLIB_MD_MEMBER(shake_256),
/* BLAKE-2 family */
Py_HASHLIB_MD_MEMBER(blake2b),
Py_HASHLIB_MD_MEMBER(blake2s),
} Py_HASHLIB_MD_NS(member);
_Py_HASHLIB_MD_MEMBER(blake2b),
_Py_HASHLIB_MD_MEMBER(blake2s),
} _Py_HASHLIB_MD_NS(member);

static const char *Py_HASHLIB_MD_NAMES[] = {
#define DECL_MESSAGE_DIGEST_NAME(ID) [Py_HASHLIB_MD_MEMBER(ID)] = #ID
#define DECL_MESSAGE_DIGEST_NAME(ID) [_Py_HASHLIB_MD_MEMBER(ID)] = #ID
/* MD-family */
DECL_MESSAGE_DIGEST_NAME(md5),
/* SHA-1 family */
Expand Down
6 changes: 4 additions & 2 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
#include "pycore_hashtable.h"
#include "pycore_strhex.h" // _Py_strhex()
#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_RELAXED

#include "_hashlib/hashlib_buffer.h"
#include "_hashlib/hashlib_fetch.h"
#include "_hashlib/hashlib_mutex.h"

/* EVP is the preferred interface to hashing in OpenSSL */
Expand Down Expand Up @@ -533,7 +535,7 @@ raise_unsupported_algorithm_error(_hashlibstate *state, PyObject *digestmod)
{
raise_unsupported_algorithm_impl(
state->unsupported_digestmod_error,
HASHLIB_UNSUPPORTED_ALGORITHM,
_Py_HASHLIB_UNSUPPORTED_ALGORITHM,
digestmod
);
}
Expand All @@ -543,7 +545,7 @@ raise_unsupported_str_algorithm_error(_hashlibstate *state, const char *name)
{
raise_unsupported_algorithm_impl(
state->unsupported_digestmod_error,
HASHLIB_UNSUPPORTED_STR_ALGORITHM,
_Py_HASHLIB_UNSUPPORTED_STR_ALGORITHM,
name
);
}
Expand Down
3 changes: 2 additions & 1 deletion Modules/hmacmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "_hacl/Hacl_Streaming_Types.h" // Hacl_Streaming_Types_error_code

#include "_hashlib/hashlib_buffer.h"
#include "_hashlib/hashlib_fetch.h"
#include "_hashlib/hashlib_mutex.h"

#include <stdbool.h>
Expand Down Expand Up @@ -657,7 +658,7 @@ find_hash_info(hmacmodule_state *state, PyObject *hash_info_ref)
}
if (rc == 0) {
PyErr_Format(state->unknown_hash_error,
HASHLIB_UNSUPPORTED_ALGORITHM, hash_info_ref);
_Py_HASHLIB_UNSUPPORTED_ALGORITHM, hash_info_ref);
return NULL;
}
assert(info != NULL);
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy