mirror of
https://github.com/python/cpython.git
synced 2025-12-08 06:10:17 +00:00
gh-129813, PEP 782: Use PyBytesWriter in _hashopenssl (#138835)
Replace PyBytes_FromStringAndSize(NULL, size) with the new public PyBytesWriter API.
This commit is contained in:
parent
ea26f6da39
commit
eec8c98d06
1 changed files with 11 additions and 13 deletions
|
|
@ -1101,20 +1101,19 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
|
||||||
/*[clinic end generated code: output=dcb09335dd2fe908 input=224d047da2c12a42]*/
|
/*[clinic end generated code: output=dcb09335dd2fe908 input=224d047da2c12a42]*/
|
||||||
{
|
{
|
||||||
EVP_MD_CTX *temp_ctx;
|
EVP_MD_CTX *temp_ctx;
|
||||||
PyObject *retval;
|
|
||||||
|
|
||||||
if (length == 0) {
|
if (length == 0) {
|
||||||
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
|
return Py_GetConstant(Py_CONSTANT_EMPTY_BYTES);
|
||||||
}
|
}
|
||||||
|
|
||||||
retval = PyBytes_FromStringAndSize(NULL, length);
|
PyBytesWriter *writer = PyBytesWriter_Create(length);
|
||||||
if (retval == NULL) {
|
if (writer == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
temp_ctx = py_wrapper_EVP_MD_CTX_new();
|
temp_ctx = py_wrapper_EVP_MD_CTX_new();
|
||||||
if (temp_ctx == NULL) {
|
if (temp_ctx == NULL) {
|
||||||
Py_DECREF(retval);
|
PyBytesWriter_Discard(writer);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1122,7 +1121,7 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
|
||||||
goto error;
|
goto error;
|
||||||
}
|
}
|
||||||
if (!EVP_DigestFinalXOF(temp_ctx,
|
if (!EVP_DigestFinalXOF(temp_ctx,
|
||||||
(unsigned char*)PyBytes_AS_STRING(retval),
|
(unsigned char*)PyBytesWriter_GetData(writer),
|
||||||
length))
|
length))
|
||||||
{
|
{
|
||||||
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestFinalXOF));
|
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_DigestFinalXOF));
|
||||||
|
|
@ -1130,10 +1129,10 @@ _hashlib_HASHXOF_digest_impl(HASHobject *self, Py_ssize_t length)
|
||||||
}
|
}
|
||||||
|
|
||||||
EVP_MD_CTX_free(temp_ctx);
|
EVP_MD_CTX_free(temp_ctx);
|
||||||
return retval;
|
return PyBytesWriter_Finish(writer);
|
||||||
|
|
||||||
error:
|
error:
|
||||||
Py_DECREF(retval);
|
PyBytesWriter_Discard(writer);
|
||||||
EVP_MD_CTX_free(temp_ctx);
|
EVP_MD_CTX_free(temp_ctx);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
@ -1750,7 +1749,6 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
|
||||||
long maxmem, long dklen)
|
long maxmem, long dklen)
|
||||||
/*[clinic end generated code: output=d424bc3e8c6b9654 input=bdeac9628d07f7a1]*/
|
/*[clinic end generated code: output=d424bc3e8c6b9654 input=bdeac9628d07f7a1]*/
|
||||||
{
|
{
|
||||||
PyObject *key = NULL;
|
|
||||||
int retval;
|
int retval;
|
||||||
|
|
||||||
if (password->len > INT_MAX) {
|
if (password->len > INT_MAX) {
|
||||||
|
|
@ -1791,8 +1789,8 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
key = PyBytes_FromStringAndSize(NULL, dklen);
|
PyBytesWriter *writer = PyBytesWriter_Create(dklen);
|
||||||
if (key == NULL) {
|
if (writer == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1801,16 +1799,16 @@ _hashlib_scrypt_impl(PyObject *module, Py_buffer *password, Py_buffer *salt,
|
||||||
(const char *)password->buf, (size_t)password->len,
|
(const char *)password->buf, (size_t)password->len,
|
||||||
(const unsigned char *)salt->buf, (size_t)salt->len,
|
(const unsigned char *)salt->buf, (size_t)salt->len,
|
||||||
(uint64_t)n, (uint64_t)r, (uint64_t)p, (uint64_t)maxmem,
|
(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
|
Py_END_ALLOW_THREADS
|
||||||
|
|
||||||
if (!retval) {
|
if (!retval) {
|
||||||
Py_DECREF(key);
|
PyBytesWriter_Discard(writer);
|
||||||
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_PBE_scrypt));
|
notify_ssl_error_occurred_in(Py_STRINGIFY(EVP_PBE_scrypt));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return key;
|
return PyBytesWriter_Finish(writer);
|
||||||
}
|
}
|
||||||
|
|
||||||
#undef HASHLIB_SCRYPT_MAX_DKLEN
|
#undef HASHLIB_SCRYPT_MAX_DKLEN
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue