FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-129813, PEP 782: Use PyBytesWriter in _sha3 by vstinner · Pull Request #138923 · python/cpython · GitHub

Repository navigation

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .c  (1) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
16 changes: 11 additions & 5 deletions Modules/sha3module.c
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -509,14 +509,19 @@ _sha3_shake_128_digest_impl(SHA3object *self, Py_ssize_t length)
if (length == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

CHECK_HACL_UINT32_T_LENGTH(length);
PyObject *digest = PyBytes_FromStringAndSize(NULL, length);
uint8_t *buffer = (uint8_t *)PyBytes_AS_STRING(digest);

PyBytesWriter *writer = PyBytesWriter_Create(length);
if (writer == NULL) {
return NULL;
}
uint8_t *buffer = (uint8_t *)PyBytesWriter_GetData(writer);

HASHLIB_ACQUIRE_LOCK(self);
(void)Hacl_Hash_SHA3_squeeze(self->hash_state, buffer, (uint32_t)length);
HASHLIB_RELEASE_LOCK(self);
return digest;

return PyBytesWriter_Finish(writer);
}


Expand All @@ -540,8 +545,8 @@ _sha3_shake_128_hexdigest_impl(SHA3object *self, Py_ssize_t length)
if (length == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_STR);
}

CHECK_HACL_UINT32_T_LENGTH(length);

uint8_t *buffer = PyMem_Malloc(length);
if (buffer == NULL) {
return PyErr_NoMemory();
Expand All @@ -550,6 +555,7 @@ _sha3_shake_128_hexdigest_impl(SHA3object *self, Py_ssize_t length)
HASHLIB_ACQUIRE_LOCK(self);
(void)Hacl_Hash_SHA3_squeeze(self->hash_state, buffer, (uint32_t)length);
HASHLIB_RELEASE_LOCK(self);

PyObject *digest = _Py_strhex((const char *)buffer, length);
PyMem_Free(buffer);
return digest;
Expand Down
Loading

Back | FazBrowse Home | New Git URL