https://github.com/python/cpython/commit/eec7a8ff22dcf409717a21a9aeab28b55526ee24
commit: eec7a8ff22dcf409717a21a9aeab28b55526ee24
branch: main
author: Bénédikt Tran <[email protected]>
committer: picnixz <[email protected]>
date: 2025-06-20T09:50:09Z
summary:

gh-135532: use `_Py_strhex` in HACL-MD5's `hexdigest` (#135742)

files:
M Modules/md5module.c

diff --git a/Modules/md5module.c b/Modules/md5module.c
index 08dbcd2cbce844..6ec0ee87a2f761 100644
--- a/Modules/md5module.c
+++ b/Modules/md5module.c
@@ -21,6 +21,8 @@
 #endif
 
 #include "Python.h"
+#include "pycore_strhex.h" // _Py_strhex()
+
 #include "hashlib.h"
 
 /*[clinic input]
@@ -136,7 +138,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 +155,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

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to