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

gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl by vstinner · Pull Request #138835 · 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
24 changes: 11 additions & 13 deletions Modules/_hashopenssl.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 @@ -1101,39 +1101,38 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
/*[clinic end generated code: output=dcb09335dd2fe908 input=224d047da2c12a42]*/
{
EVP_MD_CTX *temp_ctx;
PyObject *retval;

if (length == 0) {
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
}

retval = PyBytes_FromStringAndSize(NULL, length);
if (retval == NULL) {
PyBytesWriter *writer = PyBytesWriter_Create(length);
if (writer == NULL) {
return NULL;
}

temp_ctx = py_wrapper_EVP_MD_CTX_new();
if (temp_ctx == NULL) {
Py_DECREF(retval);
PyBytesWriter_Discard(writer);
return NULL;
}

if (_hashlib_HASH_copy_locked(self, temp_ctx) < 0) {
goto error;
}
if (!EVP_DigestFinalXOF(temp_ctx,
(unsigned char*)PyBytes_AS_STRING(retval),
(unsigned char*)PyBytesWriter_GetData(writer),
length))
{
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestFinalXOF));
goto error;
}

EVP_MD_CTX_free(temp_ctx);
return retval;
return PyBytesWriter_Finish(writer);

error:
Py_DECREF(retval);
PyBytesWriter_Discard(writer);
EVP_MD_CTX_free(temp_ctx);
return NULL;
}
Expand Down Expand Up @@ -1750,7 +1749,6 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
long maxmem, long dklen)
/*[clinic end generated code: output=d424bc3e8c6b9654 input=bdeac9628d07f7a1]*/
{
PyObject *key = NULL;
int retval;

if (password->len > INT_MAX) {
Expand Down Expand Up @@ -1791,8 +1789,8 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
return NULL;
}

key = PyBytes_FromStringAndSize(NULL, dklen);
if (key == NULL) {
PyBytesWriter *writer = PyBytesWriter_Create(dklen);
if (writer == NULL) {
return NULL;
}

Expand All @@ -1801,16 +1799,16 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
(const char *)password->buf, (size_t)password->len,
(const unsigned char *)salt->buf, (size_t)salt->len,
(uint64_t)n, (uint64_t)r, (uint64_t)p, (uint64_t)maxmem,
(unsigned char *)PyBytes_AS_STRING(key), (size_t)dklen
PyBytesWriter_GetData(writer), (size_t)dklen
);
Py_END_ALLOW_THREADS

if (!retval) {
Py_DECREF(key);
PyBytesWriter_Discard(writer);
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_PBE_scrypt));
return NULL;
}
return key;
return PyBytesWriter_Finish(writer);
}

#undef HASHLIB_SCRYPT_MAX_DKLEN
Expand Down
Loading

Back | FazBrowse Home | New Git URL