Skip to content

Commit 2e9e291

Browse files
committed
simplify md5.hexdigest computation
1 parent b53b0c1 commit 2e9e291

File tree

1 file changed

+4
-12
lines changed

1 file changed

+4
-12
lines changed

Modules/md5module.c

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

2323
#include "Python.h"
24+
#include "pycore_strhex.h" // _Py_strhex()
2425
#include "hashlib.h"
2526

2627
/*[clinic input]
@@ -136,7 +137,7 @@ static PyObject *
136137
MD5Type_digest_impl(MD5object *self)
137138
/*[clinic end generated code: output=eb691dc4190a07ec input=bc0c4397c2994be6]*/
138139
{
139-
unsigned char digest[MD5_DIGESTSIZE];
140+
uint8_t digest[MD5_DIGESTSIZE];
140141
ENTER_HASHLIB(self);
141142
Hacl_Hash_MD5_digest(self->hash_state, digest);
142143
LEAVE_HASHLIB(self);
@@ -153,20 +154,11 @@ static PyObject *
153154
MD5Type_hexdigest_impl(MD5object *self)
154155
/*[clinic end generated code: output=17badced1f3ac932 input=b60b19de644798dd]*/
155156
{
156-
unsigned char digest[MD5_DIGESTSIZE];
157+
uint8_t digest[MD5_DIGESTSIZE];
157158
ENTER_HASHLIB(self);
158159
Hacl_Hash_MD5_digest(self->hash_state, digest);
159160
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));
161+
return _Py_strhex((const char *)digest, MD5_DIGESTSIZE);
170162
}
171163

172164
static void

0 commit comments

Comments
 (0)