QMessageAuthenticationCode: port to QByteArrayView [1/3]: ctor/setKey()

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. QCryptographicHash also
doesn't have it.

Also mark setKey() noexcept. Now that it takes a view instead of
an owning container, it only calls other noexcept functions.

ChangeLog will be on patch [3/3].

Task-number: QTBUG-111676
Task-number: QTBUG-111688
Change-Id: Ic2321d0d41ce8eb4d0390889f28b3fd4bd9af103
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2023-03-02 19:05:16 +01:00
parent 0cab9b56e9
commit 93cb61e305
3 changed files with 33 additions and 3 deletions

View File

@ -486,6 +486,17 @@ void QXmlStreamWriter::writeStartElement(const QString &namespaceUri, const QStr
#if QT_CORE_REMOVED_SINCE(6, 6)
#include "qmessageauthenticationcode.h"
QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
const QByteArray &key)
: QMessageAuthenticationCode(method, qToByteArrayViewIgnoringNull(key)) {}
void QMessageAuthenticationCode::setKey(const QByteArray &key)
{
setKey(qToByteArrayViewIgnoringNull(key));
}
#include "qstring.h"
qsizetype QString::toUcs4_helper(const ushort *uc, qsizetype length, uint *out)

View File

@ -1245,9 +1245,18 @@ void QMessageAuthenticationCodePrivate::initMessageHash() noexcept
/*!
Constructs an object that can be used to create a cryptographic hash from data
using method \a method and key \a key.
//! [qba-to-qbav-6.6]
\note In Qt versions prior to 6.6, this function took its arguments as
QByteArray, not QByteArrayView. If you experience compile errors, it's
because your code is passing objects that are implicitly convertible to
QByteArray, but not QByteArrayView. Wrap the corresponding argument in
\c{QByteArray{~~~}} to make the cast explicit. This is backwards-compatible
with old Qt versions.
//! [qba-to-qbav-6.6]
*/
QMessageAuthenticationCode::QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
const QByteArray &key)
QByteArrayView key)
: d(new QMessageAuthenticationCodePrivate(method))
{
d->setKey(key);
@ -1334,8 +1343,10 @@ void QMessageAuthenticationCode::reset()
mac.emplace(method, key);
use(*mac);
\endcode
\include qcryptographichash.cpp {qba-to-qbav-6.6}
*/
void QMessageAuthenticationCode::setKey(const QByteArray &key)
void QMessageAuthenticationCode::setKey(QByteArrayView key) noexcept
{
d->result.clear();
d->messageHash.reset();

View File

@ -16,8 +16,13 @@ class QIODevice;
class Q_CORE_EXPORT QMessageAuthenticationCode
{
public:
#if QT_CORE_REMOVED_SINCE(6, 6)
explicit QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
const QByteArray &key = QByteArray());
const QByteArray &key);
#endif
explicit QMessageAuthenticationCode(QCryptographicHash::Algorithm method,
QByteArrayView key = {});
QMessageAuthenticationCode(QMessageAuthenticationCode &&other) noexcept
: d{std::exchange(other.d, nullptr)} {}
~QMessageAuthenticationCode();
@ -28,7 +33,10 @@ public:
void reset();
#if QT_CORE_REMOVED_SINCE(6, 6)
void setKey(const QByteArray &key);
#endif
void setKey(QByteArrayView key) noexcept;
void addData(const char *data, qsizetype length);
void addData(const QByteArray &data);