From d1a7e9f4934f52e6b17a6e0732dab0aecf25bc0a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Tue, 21 Feb 2023 09:11:34 +0100 Subject: [PATCH] QMessageAuthenticationCode: Extract Methods finalize{,Unchecked}() from result() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This brings the code in line with its sibling code in QCryptographicHash and prepares for a static hash() optimization and the fixing of the result() re-entrancy issue (QTBUG-111347). Task-number: QTBUG-111347 Change-Id: I3d0c0cd2a37c2bbeb60974307ff138e26b82bf69 Reviewed-by: Fabian Kosmale Reviewed-by: MÃ¥rten Nordheim (cherry picked from commit ac9d25340aec5c165db5e58a3d8b869967c5ff97) Reviewed-by: Qt Cherry-pick Bot --- .../tools/qmessageauthenticationcode.cpp | 29 +++++++++++++------ 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/src/corelib/tools/qmessageauthenticationcode.cpp b/src/corelib/tools/qmessageauthenticationcode.cpp index 4ff48fc822e..8f131e45a07 100644 --- a/src/corelib/tools/qmessageauthenticationcode.cpp +++ b/src/corelib/tools/qmessageauthenticationcode.cpp @@ -75,6 +75,9 @@ public: bool messageHashInited; void initMessageHash(); + void finalize(); + + void finalizeUnchecked(); }; void QMessageAuthenticationCodePrivate::initMessageHash() @@ -208,27 +211,35 @@ bool QMessageAuthenticationCode::addData(QIODevice *device) */ QByteArray QMessageAuthenticationCode::result() const { - if (!d->result.isEmpty()) - return d->result; + d->finalize(); + return d->result; +} - d->initMessageHash(); +void QMessageAuthenticationCodePrivate::finalize() +{ + if (!result.isEmpty()) + return; + initMessageHash(); + finalizeUnchecked(); +} - const int blockSize = qt_hash_block_size(d->method); +void QMessageAuthenticationCodePrivate::finalizeUnchecked() +{ + const int blockSize = qt_hash_block_size(method); - QByteArrayView hashedMessage = d->messageHash.resultView(); + QByteArrayView hashedMessage = messageHash.resultView(); QVarLengthArray oKeyPad(blockSize); - const char * const keyData = d->key.constData(); + const char * const keyData = key.constData(); for (int i = 0; i < blockSize; ++i) oKeyPad[i] = keyData[i] ^ 0x5c; - QCryptographicHash hash(d->method); + QCryptographicHash hash(method); hash.addData(oKeyPad); hash.addData(hashedMessage); - d->result = hash.result(); - return d->result; + result = hash.result(); } /*!