From 289a54a809d08dbe3d4175afdb5375a0fa7e8baa Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 23 Feb 2023 16:01:20 +0100 Subject: [PATCH] QMessageAuthenticationCode: reuse messageHash in finalizeUnchecked() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The only reason why we couldn't before was because we had a view on messageHash's result, which would have been clobbered when resetting messageHash for reuse. Since QMessageAuthenticationCodePrivate now contains QCryptographicHashPrivate, we gain access to the latter's QSmallByteArray result, and can just take a copy before reset()ing. Re-using a QCryptographicHash is faster than creating a new one, esp. in OpenSSL3 mode, where construction allocates a context while reset() no longer does. Pick-to: 6.5 Change-Id: I3bc0454918840a104fd53909e79b6fe21326bfbf Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/tools/qcryptographichash.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index ef5100c359..19207ffb86 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -1206,7 +1206,7 @@ void QMessageAuthenticationCodePrivate::finalizeUnchecked() const int blockSize = qt_hash_block_size(method); messageHash.finalizeUnchecked(); - QByteArrayView hashedMessage = messageHash.resultView(); + const HashResult hashedMessage = messageHash.result; QVarLengthArray oKeyPad(blockSize); const char * const keyData = key.constData(); @@ -1214,10 +1214,10 @@ void QMessageAuthenticationCodePrivate::finalizeUnchecked() for (int i = 0; i < blockSize; ++i) oKeyPad[i] = keyData[i] ^ 0x5c; - QCryptographicHashPrivate hash(method); - hash.addData(oKeyPad); - hash.addData(hashedMessage); - hash.finalizeUnchecked(); + messageHash.reset(); + messageHash.addData(oKeyPad); + messageHash.addData(hashedMessage.toByteArrayView()); + messageHash.finalizeUnchecked(); result = hash.resultView().toByteArray(); }