From 6990f23813bc14be791838949dd93b5b33b40b77 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 23 Feb 2023 21:13:25 +0100 Subject: [PATCH] Long live QMessageAuthenticationCode::resultView()! MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Use it in a few places. [ChangeLog][QtCore][QMessageAuthenticationCode] Added QCryptographicHash-style resultView(). Change-Id: I745d71f86f9c19c9a9aabb2021c6617775dab1cf Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Qt CI Bot --- src/corelib/tools/qcryptographichash.cpp | 21 ++++++++++++++++--- .../tools/qmessageauthenticationcode.h | 1 + src/plugins/tls/openssl/qdtls_openssl.cpp | 2 +- .../tst_qmessageauthenticationcode.cpp | 12 +++++------ 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 86e1de7059..8ada1277b6 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -1331,15 +1331,30 @@ bool QMessageAuthenticationCode::addData(QIODevice *device) return d->messageHash.addData(device); } +/*! + \since 6.6 + + Returns the final hash value. + + Note that the returned view remains valid only as long as the + QMessageAuthenticationCode object is not modified by other means. + + \sa result() +*/ +QByteArrayView QMessageAuthenticationCode::resultView() const noexcept +{ + d->finalize(); + return d->result.toByteArrayView(); +} + /*! Returns the final authentication code. - \sa QByteArray::toHex() + \sa resultView(), QByteArray::toHex() */ QByteArray QMessageAuthenticationCode::result() const { - d->finalize(); - return d->result.toByteArrayView().toByteArray(); + return resultView().toByteArray(); } void QMessageAuthenticationCodePrivate::finalize() diff --git a/src/corelib/tools/qmessageauthenticationcode.h b/src/corelib/tools/qmessageauthenticationcode.h index 3951a6e5fa..a7fb13d201 100644 --- a/src/corelib/tools/qmessageauthenticationcode.h +++ b/src/corelib/tools/qmessageauthenticationcode.h @@ -28,6 +28,7 @@ public: void addData(const QByteArray &data); bool addData(QIODevice *device); + QByteArrayView resultView() const noexcept; QByteArray result() const; static QByteArray hash(const QByteArray &message, const QByteArray &key, diff --git a/src/plugins/tls/openssl/qdtls_openssl.cpp b/src/plugins/tls/openssl/qdtls_openssl.cpp index d34cfe7c64..78288d8dd1 100644 --- a/src/plugins/tls/openssl/qdtls_openssl.cpp +++ b/src/plugins/tls/openssl/qdtls_openssl.cpp @@ -182,7 +182,7 @@ extern "C" int q_generate_cookie_callback(SSL *ssl, unsigned char *dst, QMessageAuthenticationCode hmac(dtls->hashAlgorithm, dtls->secret); hmac.addData(peerData); - const QByteArray cookie = hmac.result(); + const QByteArrayView cookie = hmac.resultView(); Q_ASSERT(cookie.size() >= 0); // DTLS1_COOKIE_LENGTH is erroneously 256 bytes long, must be 255 - RFC 6347, 4.2.1. *cookieLength = qMin(DTLS1_COOKIE_LENGTH - 1, cookie.size()); diff --git a/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp b/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp index 59fecc1b56..002f6f587a 100644 --- a/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp +++ b/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp @@ -148,11 +148,11 @@ void tst_QMessageAuthenticationCode::result() QMessageAuthenticationCode mac(algo); mac.setKey(key); mac.addData(message); - QByteArray result = mac.result(); + QByteArrayView resultView = mac.resultView(); - QCOMPARE(result, code); + QCOMPARE(resultView, code); - result = QMessageAuthenticationCode::hash(message, key, algo); + const auto result = QMessageAuthenticationCode::hash(message, key, algo); QCOMPARE(result, code); } @@ -178,7 +178,7 @@ void tst_QMessageAuthenticationCode::result_incremental() mac.setKey(key); mac.addData(leftPart); mac.addData(rightPart); - QByteArray result = mac.result(); + QByteArrayView result = mac.resultView(); QCOMPARE(result, code); } @@ -200,7 +200,7 @@ void tst_QMessageAuthenticationCode::addData_overloads() QMessageAuthenticationCode mac(algo); mac.setKey(key); mac.addData(message.constData(), message.size()); - QByteArray result = mac.result(); + QByteArrayView result = mac.resultView(); QCOMPARE(result, code); } @@ -212,7 +212,7 @@ void tst_QMessageAuthenticationCode::addData_overloads() QMessageAuthenticationCode mac(algo); mac.setKey(key); QVERIFY(mac.addData(&buffer)); - QByteArray result = mac.result(); + QByteArrayView result = mac.resultView(); buffer.close(); QCOMPARE(result, code);