From 2e9e2911e198c2df46dbeac3b6e76365996d9205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Fri, 20 Jun 2025 10:36:10 +0200 Subject: [PATCH 1/2] simplify `md5.hexdigest` computation --- Modules/md5module.c | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/Modules/md5module.c b/Modules/md5module.c index 08dbcd2cbce844..f6be73837dbeaa 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -21,6 +21,7 @@ #endif #include "Python.h" +#include "pycore_strhex.h" // _Py_strhex() #include "hashlib.h" /*[clinic input] @@ -136,7 +137,7 @@ static PyObject * MD5Type_digest_impl(MD5object *self) /*[clinic end generated code: output=eb691dc4190a07ec input=bc0c4397c2994be6]*/ { - unsigned char digest[MD5_DIGESTSIZE]; + uint8_t digest[MD5_DIGESTSIZE]; ENTER_HASHLIB(self); Hacl_Hash_MD5_digest(self->hash_state, digest); LEAVE_HASHLIB(self); @@ -153,20 +154,11 @@ static PyObject * MD5Type_hexdigest_impl(MD5object *self) /*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/ { - unsigned char digest[MD5_DIGESTSIZE]; + uint8_t digest[MD5_DIGESTSIZE]; ENTER_HASHLIB(self); Hacl_Hash_MD5_digest(self->hash_state, digest); LEAVE_HASHLIB(self); - - const char *hexdigits = "0123456789abcdef"; - char digest_hex[MD5_DIGESTSIZE * 2]; - char *str = digest_hex; - for (size_t i=0; i < MD5_DIGESTSIZE; i++) { - unsigned char byte = digest[i]; - *str++ = hexdigits[byte >> 4]; - *str++ = hexdigits[byte & 0x0f]; - } - return PyUnicode_FromStringAndSize(digest_hex, sizeof(digest_hex)); + return _Py_strhex((const char *)digest, MD5_DIGESTSIZE); } static void From d2ab3025f959a56e9de4613bf2b5e808c5115f74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?B=C3=A9n=C3=A9dikt=20Tran?= <10796600+picnixz@users.noreply.github.com> Date: Fri, 20 Jun 2025 10:51:08 +0200 Subject: [PATCH 2/2] separate global/local imports --- Modules/md5module.c | 1 + 1 file changed, 1 insertion(+) diff --git a/Modules/md5module.c b/Modules/md5module.c index f6be73837dbeaa..6ec0ee87a2f761 100644 --- a/Modules/md5module.c +++ b/Modules/md5module.c @@ -22,6 +22,7 @@ #include "Python.h" #include "pycore_strhex.h" // _Py_strhex() + #include "hashlib.h" /*[clinic input]