diff --git a/Include/internal/pycore_global_objects_fini_generated.h b/Include/internal/pycore_global_objects_fini_generated.h index 572fbe0a983c36..d8b8e61433630b 100644 --- a/Include/internal/pycore_global_objects_fini_generated.h +++ b/Include/internal/pycore_global_objects_fini_generated.h @@ -631,6 +631,7 @@ _PyStaticObjects_CheckAll(PyInterpreterState *interp) { _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(locals), "locals", 6); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(logoption), "logoption", 9); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(loop), "loop", 4); + _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(lower), "lower", 5); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(manual_reset), "manual_reset", 12); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(mapping), "mapping", 7); _PyStaticObject_CheckUnicodeSingleton((PyObject *)&_Py_ID(markers), "markers", 7); diff --git a/Include/internal/pycore_global_strings.h b/Include/internal/pycore_global_strings.h index 4b5f9fa731d8e4..984d9c260bc734 100644 --- a/Include/internal/pycore_global_strings.h +++ b/Include/internal/pycore_global_strings.h @@ -643,6 +643,7 @@ struct _Py_global_strings { STRUCT_FOR_ID(locals) STRUCT_FOR_ID(logoption) STRUCT_FOR_ID(loop) + STRUCT_FOR_ID(lower) STRUCT_FOR_ID(manual_reset) STRUCT_FOR_ID(mapping) STRUCT_FOR_ID(markers) diff --git a/Include/internal/pycore_runtime_init_generated.h b/Include/internal/pycore_runtime_init_generated.h index 479310bdc2d896..3efa68182220b9 100644 --- a/Include/internal/pycore_runtime_init_generated.h +++ b/Include/internal/pycore_runtime_init_generated.h @@ -1918,6 +1918,7 @@ extern "C" { INIT_ID(locals), \ INIT_ID(logoption), \ INIT_ID(loop), \ + INIT_ID(lower), \ INIT_ID(manual_reset), \ INIT_ID(mapping), \ INIT_ID(markers), \ diff --git a/Include/internal/pycore_unicodeobject_generated.h b/Include/internal/pycore_unicodeobject_generated.h index 2db08616edd693..a7ef912b1a4362 100644 --- a/Include/internal/pycore_unicodeobject_generated.h +++ b/Include/internal/pycore_unicodeobject_generated.h @@ -595,6 +595,7 @@ _PyUnicode_InitStaticStrings(PyInterpreterState *interp) { offsetof(struct _Py_global_strings, identifiers._py_locals._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_logoption._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_loop._ascii.ob_base), + offsetof(struct _Py_global_strings, identifiers._py_lower._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_manual_reset._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_mapping._ascii.ob_base), offsetof(struct _Py_global_strings, identifiers._py_markers._ascii.ob_base), diff --git a/Lib/test/test_hashlib.py b/Lib/test/test_hashlib.py index eff95fd8aa84f6..d8c49736c89744 100644 --- a/Lib/test/test_hashlib.py +++ b/Lib/test/test_hashlib.py @@ -377,6 +377,15 @@ def test_unknown_hash(self): def test_new_upper_to_lower(self): self.assertEqual(hashlib.new("SHA256").name, "sha256") + @hashlib_helper.requires_openssl_hashdigest("SHA256-192") + def test_name_attribute_without_nid(self): + # OpenSSL 3 provider digest without NID. + # See https://github.com/python/cpython/issues/162712. + h = hashlib.new("SHA256-192") + self.assertNotEqual(h.name, "undefined") + self.assertEqual(h.name, h.name.lower()) + self.assertEqual(hashlib.new(h.name).name, h.name) + @support.thread_unsafe("modifies sys.modules") def test_get_builtin_constructor(self): get_builtin_constructor = getattr(hashlib, diff --git a/Misc/NEWS.d/next/Library/2026-12-02-12-00-00.gh-issue-162712.r4Xi3w.rst b/Misc/NEWS.d/next/Library/2026-12-02-12-00-00.gh-issue-162712.r4Xi3w.rst new file mode 100644 index 00000000000000..b905063f1981f6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-12-02-12-00-00.gh-issue-162712.r4Xi3w.rst @@ -0,0 +1,4 @@ +:mod:`hashlib`: the :attr:`name ` of OpenSSL hash objects whose +digest has no OpenSSL NID, such as ``SHA256-192``, is now the lowercase name +given by the OpenSSL provider instead of ``"undefined"``. +Patch by Bénédikt Tran. diff --git a/Modules/_hashopenssl.c b/Modules/_hashopenssl.c index 9d7602b39d7b93..d5c2f8e17dee80 100644 --- a/Modules/_hashopenssl.c +++ b/Modules/_hashopenssl.c @@ -23,7 +23,9 @@ #endif #include "Python.h" +#include "pycore_global_strings.h" // _Py_ID() #include "pycore_hashtable.h" +#include "pycore_runtime.h" // _PyRuntime #include "pycore_strhex.h" // _Py_strhex() #include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_RELAXED #include "hashlib.h" @@ -656,13 +658,46 @@ get_openssl_utf8name_by_nid(int nid) } #endif -/* Same as get_hashlib_utf8name_by_nid() but using an EVP_MD object. */ +/* + * Same as get_hashlib_utf8name_by_nid() but using an EVP_MD object. + * + * With OpenSSL 3, a provider digest without NID, such as SHA2-256/192, + * is named by its provider, if any, and is named as NID_undef otherwise. + */ static const char * get_hashlib_utf8name_by_evp_md(const EVP_MD *md) { assert(md != NULL); - return get_hashlib_utf8name_by_nid(EVP_MD_nid(md)); + int nid = EVP_MD_nid(md); +#ifdef Py_HAS_OPENSSL3_SUPPORT + if (nid == NID_undef) { + const char *name = EVP_MD_get0_name(md); + if (name != NULL) { + return name; + } + } +#endif + return get_hashlib_utf8name_by_nid(nid); +} + +#ifdef Py_HAS_OPENSSL3_SUPPORT +/* + * Convert the UTF-8 name of a provider digest into a lowercase string. + * + * On error, set an exception and return NULL. + */ +static PyObject * +get_hashlib_lowercase_name(const char *name) +{ + PyObject *str = PyUnicode_FromString(name); + if (str == NULL) { + return NULL; + } + PyObject *res = PyObject_CallMethodNoArgs(str, &_Py_ID(lower)); + Py_DECREF(str); + return res; } +#endif /* * Return 1 if the property query clause [1] must be "-fips" and 0 otherwise. @@ -1043,7 +1078,15 @@ _hashlib_HASH_get_name(PyObject *op, void *Py_UNUSED(closure)) } const char *name = get_hashlib_utf8name_by_evp_md(md); assert(name != NULL || PyErr_Occurred()); - return name == NULL ? NULL : PyUnicode_FromString(name); + if (name == NULL) { + return NULL; + } +#ifdef Py_HAS_OPENSSL3_SUPPORT + if (EVP_MD_nid(md) == NID_undef) { + return get_hashlib_lowercase_name(name); + } +#endif + return PyUnicode_FromString(name); } static PyGetSetDef HASH_getsets[] = {