Long live QMessageAuthenticationCode::resultView()!

Use it in a few places.

[ChangeLog][QtCore][QMessageAuthenticationCode] Added
QCryptographicHash-style resultView().

Change-Id: I745d71f86f9c19c9a9aabb2021c6617775dab1cf
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Marc Mutz 2023-02-23 21:13:25 +01:00
parent 7af39be052
commit 6990f23813
4 changed files with 26 additions and 10 deletions

View File

@ -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()

View File

@ -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,

View File

@ -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());

View File

@ -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);