From c19f9716fb06a0bff26ec3f2c5104b60e6cd245a Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 2 Mar 2023 19:05:16 +0100 Subject: [PATCH] QMessageAuthenticationCode: port to QByteArrayView [3/3]: static hash() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The class is not used frequently enough to warrant the overhead of a Q_WEAK_ QByteArray overload to fix the SiC Type A for users that pass objects that implicitly convert to QByteArray, esp. since we'd need _four_ overloads to handle the two byte array arguments here, and still cause ambiguities, because there's only one level of "weakness" in Q_WEAK_OVERLOAD, but we'd need two. QCryptographicHash::hash() also doesn't have it. [ChangeLog][QtCore][QMessageAuthenticationCode] The constructor, setKey(), addData() methods as well as the static hash() function take arguments by QByteArrayView instead of QByteArray now. [ChangeLog][Potentially Source-Incompatible Changes] More APIs now take QByteArrayView instead of QByteArray. You will now get a compile error when your code passes to such functions objects that implicitly convert to QByteArray, but not QByteArrayView. Wrapping such arguments in QByteArray{~~~} to make the cast explicit is a backwards-compatible way to avoid this problem. Fixes: QTBUG-111676 Change-Id: Iaff832df3b315ca2eee7bff93720b6681182036f Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira Reviewed-by: MÃ¥rten Nordheim --- src/corelib/compat/removed_api.cpp | 7 +++++++ src/corelib/tools/qcryptographichash.cpp | 4 +++- src/corelib/tools/qmessageauthenticationcode.h | 4 ++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/corelib/compat/removed_api.cpp b/src/corelib/compat/removed_api.cpp index dd9a13ff355..fdbe5de41c1 100644 --- a/src/corelib/compat/removed_api.cpp +++ b/src/corelib/compat/removed_api.cpp @@ -502,6 +502,13 @@ void QMessageAuthenticationCode::addData(const QByteArray &data) addData(qToByteArrayViewIgnoringNull(data)); } +QByteArray QMessageAuthenticationCode::hash(const QByteArray &msg, const QByteArray &key, + QCryptographicHash::Algorithm method) +{ + return hash(qToByteArrayViewIgnoringNull(msg), + qToByteArrayViewIgnoringNull(key), method); +} + #include "qstring.h" qsizetype QString::toUcs4_helper(const ushort *uc, qsizetype length, uint *out) diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index ac0ee44860e..8f616e966d1 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -1436,8 +1436,10 @@ void QMessageAuthenticationCodePrivate::finalizeUnchecked() noexcept /*! Returns the authentication code for the message \a message using the key \a key and the method \a method. + + \include qcryptographichash.cpp {qba-to-qbav-6.6} */ -QByteArray QMessageAuthenticationCode::hash(const QByteArray &message, const QByteArray &key, +QByteArray QMessageAuthenticationCode::hash(QByteArrayView message, QByteArrayView key, QCryptographicHash::Algorithm method) { QMessageAuthenticationCodePrivate mac(method); diff --git a/src/corelib/tools/qmessageauthenticationcode.h b/src/corelib/tools/qmessageauthenticationcode.h index 4fd811f74b8..9e7f9dfbdb0 100644 --- a/src/corelib/tools/qmessageauthenticationcode.h +++ b/src/corelib/tools/qmessageauthenticationcode.h @@ -48,8 +48,12 @@ public: QByteArrayView resultView() const noexcept; QByteArray result() const; +#if QT_CORE_REMOVED_SINCE(6, 6) static QByteArray hash(const QByteArray &message, const QByteArray &key, QCryptographicHash::Algorithm method); +#endif + static QByteArray hash(QByteArrayView message, QByteArrayView key, + QCryptographicHash::Algorithm method); private: Q_DISABLE_COPY(QMessageAuthenticationCode)