Skip to content

Commit eec7a8f

Browse files
authored
gh-135532: use _Py_strhex in HACL-MD5's hexdigest (#135742)
1 parent 57dba7c commit eec7a8f

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

Modules/md5module.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#endif
2222

2323
#include "Python.h"
24+
#include "pycore_strhex.h" // _Py_strhex()
25+
2426
#include "hashlib.h"
2527

2628
/*[clinic input]
@@ -136,7 +138,7 @@ static PyObject *
136138
MD5Type_digest_impl(MD5object *self)
137139
/*[clinic end generated code: output=eb691dc4190a07ec input=bc0c4397c2994be6]*/
138140
{
139-
unsigned char digest[MD5_DIGESTSIZE];
141+
uint8_t digest[MD5_DIGESTSIZE];
140142
ENTER_HASHLIB(self);
141143
Hacl_Hash_MD5_digest(self->hash_state, digest);
142144
LEAVE_HASHLIB(self);
@@ -153,20 +155,11 @@ static PyObject *
153155
MD5Type_hexdigest_impl(MD5object *self)
154156
/*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/
155157
{
156-
unsigned char digest[MD5_DIGESTSIZE];
158+
uint8_t digest[MD5_DIGESTSIZE];
157159
ENTER_HASHLIB(self);
158160
Hacl_Hash_MD5_digest(self->hash_state, digest);
159161
LEAVE_HASHLIB(self);
160-
161-
const char *hexdigits = "0123456789abcdef";
162-
char digest_hex[MD5_DIGESTSIZE * 2];
163-
char *str = digest_hex;
164-
for (size_t i=0; i < MD5_DIGESTSIZE; i++) {
165-
unsigned char byte = digest[i];
166-
*str++ = hexdigits[byte >> 4];
167-
*str++ = hexdigits[byte & 0x0f];
168-
}
169-
return PyUnicode_FromStringAndSize(digest_hex, sizeof(digest_hex));
162+
return _Py_strhex((const char *)digest, MD5_DIGESTSIZE);
170163
}
171164

172165
static void

0 commit comments

Comments
 (0)