@@ -509,14 +509,19 @@ _sha3_shake_128_digest_impl(SHA3object *self, Py_ssize_t length)
509
509
if (length == 0 ) {
510
510
return Py_GetConstant (Py_CONSTANT_EMPTY_BYTES );
511
511
}
512
-
513
512
CHECK_HACL_UINT32_T_LENGTH (length );
514
- PyObject * digest = PyBytes_FromStringAndSize (NULL , length );
515
- uint8_t * buffer = (uint8_t * )PyBytes_AS_STRING (digest );
513
+
514
+ PyBytesWriter * writer = PyBytesWriter_Create (length );
515
+ if (writer == NULL ) {
516
+ return NULL ;
517
+ }
518
+ uint8_t * buffer = (uint8_t * )PyBytesWriter_GetData (writer );
519
+
516
520
HASHLIB_ACQUIRE_LOCK (self );
517
521
(void )Hacl_Hash_SHA3_squeeze (self -> hash_state , buffer , (uint32_t )length );
518
522
HASHLIB_RELEASE_LOCK (self );
519
- return digest ;
523
+
524
+ return PyBytesWriter_Finish (writer );
520
525
}
521
526
522
527
@@ -540,8 +545,8 @@ _sha3_shake_128_hexdigest_impl(SHA3object *self, Py_ssize_t length)
540
545
if (length == 0 ) {
541
546
return Py_GetConstant (Py_CONSTANT_EMPTY_STR );
542
547
}
543
-
544
548
CHECK_HACL_UINT32_T_LENGTH (length );
549
+
545
550
uint8_t * buffer = PyMem_Malloc (length );
546
551
if (buffer == NULL ) {
547
552
return PyErr_NoMemory ();
@@ -550,6 +555,7 @@ _sha3_shake_128_hexdigest_impl(SHA3object *self, Py_ssize_t length)
550
555
HASHLIB_ACQUIRE_LOCK (self );
551
556
(void )Hacl_Hash_SHA3_squeeze (self -> hash_state , buffer , (uint32_t )length );
552
557
HASHLIB_RELEASE_LOCK (self );
558
+
553
559
PyObject * digest = _Py_strhex ((const char * )buffer , length );
554
560
PyMem_Free (buffer );
555
561
return digest ;
0 commit comments