From d2e1c0aef1ebf6dde2312978537ba9c868f573bc Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 23 Feb 2023 13:58:18 +0100 Subject: [PATCH] QMessageAuthenticationCode: re-use messageHash for hashing the key With the OpenSSL3 code allocating state on the heap instead of in QCH::Private's inline union, reset() should be faster than even a static hash() call. Even in the non-OpenSSL3 case, using less QCH::Private objects to do the same thing means we increase effective data cache size. Pick-to: 6.5 Change-Id: I0b1347864081169a24c5d349702931afdab6c5bf Reviewed-by: Thiago Macieira --- src/corelib/tools/qcryptographichash.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 95498df3d9..cda3c9612a 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -1002,12 +1002,22 @@ public: // END functions that need to be called with finalizeMutex held }; +/*! + \internal + + Transforms key into a block-sized format and then seeds messageHash from it. + + This function assumes that messageHash is in its initial state (reset() has + been called). +*/ void QMessageAuthenticationCodePrivate::initMessageHash() { const int blockSize = qt_hash_block_size(method); if (key.size() > blockSize) { - key = QCryptographicHash::hash(key, method); + messageHash.addData(key); + key = messageHash.result(); + messageHash.reset(); } if (key.size() < blockSize) {