diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 497b9f9b4a..3aedc06730 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -1262,6 +1262,39 @@ QMessageAuthenticationCode::~QMessageAuthenticationCode() delete d; } +/*! + \fn QMessageAuthenticationCode::QMessageAuthenticationCode(QMessageAuthenticationCode &&other) + + Move-constructs a new QMessageAuthenticationCode from \a other. + + \note The moved-from object \a other is placed in a + partially-formed state, in which the only valid operations are + destruction and assignment of a new object. + + \since 6.6 +*/ + +/*! + \fn QMessageAuthenticationCode &QMessageAuthenticationCode::operator=(QMessageAuthenticationCode &&other) + + Move-assigns \a other to this QMessageAuthenticationCode instance. + + \note The moved-from object \a other is placed in a + partially-formed state, in which the only valid operations are + destruction and assignment of a new object. + + \since 6.6 +*/ + +/*! + \fn void QMessageAuthenticationCode::swap(QMessageAuthenticationCode &other) + + Swaps message authentication code \a other with this message authentication + code. This operation is very fast and never fails. + + \since 6.6 +*/ + /*! Resets message data. Calling this method doesn't affect the key. */ diff --git a/src/corelib/tools/qmessageauthenticationcode.h b/src/corelib/tools/qmessageauthenticationcode.h index a7fb13d201..6be8682332 100644 --- a/src/corelib/tools/qmessageauthenticationcode.h +++ b/src/corelib/tools/qmessageauthenticationcode.h @@ -18,8 +18,14 @@ class Q_CORE_EXPORT QMessageAuthenticationCode public: explicit QMessageAuthenticationCode(QCryptographicHash::Algorithm method, const QByteArray &key = QByteArray()); + QMessageAuthenticationCode(QMessageAuthenticationCode &&other) noexcept + : d{std::exchange(other.d, nullptr)} {} ~QMessageAuthenticationCode(); + QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QMessageAuthenticationCode) + void swap(QMessageAuthenticationCode &other) noexcept + { qt_ptr_swap(d, other.d); } + void reset(); void setKey(const QByteArray &key); diff --git a/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp b/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp index 002f6f587a..85138a6402 100644 --- a/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp +++ b/tests/auto/corelib/tools/qmessageauthenticationcode/tst_qmessageauthenticationcode.cpp @@ -20,6 +20,8 @@ private slots: void result_incremental(); void addData_overloads_data(); void addData_overloads(); + void move(); + void swap(); }; Q_DECLARE_METATYPE(QCryptographicHash::Algorithm) @@ -219,5 +221,48 @@ void tst_QMessageAuthenticationCode::addData_overloads() } } +void tst_QMessageAuthenticationCode::move() +{ + const QByteArray key = "123"; + + QMessageAuthenticationCode src(QCryptographicHash::Sha1, key); + src.addData("a"); + + // move constructor + auto intermediary = std::move(src); + intermediary.addData("b"); + + // move assign operator + QMessageAuthenticationCode dst(QCryptographicHash::Sha256, key); + dst.addData("no effect on the end result"); + dst = std::move(intermediary); + dst.addData("c"); + + QCOMPARE(dst.resultView(), + QMessageAuthenticationCode::hash("abc", key, QCryptographicHash::Sha1)); +} + +void tst_QMessageAuthenticationCode::swap() +{ + const QByteArray key1 = "123"; + const QByteArray key2 = "abcdefg"; + + QMessageAuthenticationCode mac1(QCryptographicHash::Sha1, key1); + QMessageAuthenticationCode mac2(QCryptographicHash::Sha256, key2); + + mac1.addData("da"); + mac2.addData("te"); + + mac1.swap(mac2); + + mac2.addData("ta"); + mac1.addData("st"); + + QCOMPARE(mac2.resultView(), + QMessageAuthenticationCode::hash("data", key1, QCryptographicHash::Sha1)); + QCOMPARE(mac1.resultView(), + QMessageAuthenticationCode::hash("test", key2, QCryptographicHash::Sha256)); +} + QTEST_MAIN(tst_QMessageAuthenticationCode) #include "tst_qmessageauthenticationcode.moc"