diff --git a/src/corelib/tools/qcryptographichash.cpp b/src/corelib/tools/qcryptographichash.cpp index 8baec8ecc8b..3807e32a723 100644 --- a/src/corelib/tools/qcryptographichash.cpp +++ b/src/corelib/tools/qcryptographichash.cpp @@ -1153,13 +1153,42 @@ void QCryptographicHashPrivate::State::finalizeUnchecked(QCryptographicHash::Alg \note In Qt versions prior to 6.3, this function took QByteArray, not QByteArrayView. + + \sa hashInto() */ QByteArray QCryptographicHash::hash(QByteArrayView data, Algorithm method) +{ + QByteArray ba(hashLengthInternal(method), Qt::Uninitialized); + [[maybe_unused]] const auto r = hashInto(ba, data, method); + Q_ASSERT(r.size() == ba.size()); + return ba; +} + +/*! + \since 6.8 + \fn QCryptographicHash::hashInto(QSpan buffer, QByteArrayView data, Algorithm method); + \fn QCryptographicHash::hashInto(QSpan buffer, QByteArrayView data, Algorithm method); + \fn QCryptographicHash::hashInto(QSpan buffer, QByteArrayView data, Algorithm method); + + Returns the hash of \a data using \a method, using \a buffer to store the result. + + The return value will be a sub-span of \a buffer, unless \a buffer is of + insufficient size, in which case a null QByteArrayView is returned. + + \sa hash() +*/ +QByteArrayView QCryptographicHash::hashInto(QSpan buffer, QByteArrayView data, + Algorithm method) noexcept { QCryptographicHashPrivate hash(method); hash.addData(data); hash.finalizeUnchecked(); // no mutex needed: no-one but us has access to 'hash' - return hash.resultView().toByteArray(); + auto result = hash.resultView(); + if (buffer.size() < result.size()) + return {}; // buffer too small + // ### optimize: have the method directly write into `buffer` + memcpy(buffer.data(), result.data(), result.size()); + return buffer.first(result.size()); } /*! diff --git a/src/corelib/tools/qcryptographichash.h b/src/corelib/tools/qcryptographichash.h index 294453adceb..3d91c1f917c 100644 --- a/src/corelib/tools/qcryptographichash.h +++ b/src/corelib/tools/qcryptographichash.h @@ -8,6 +8,7 @@ #include #include +#include QT_BEGIN_NAMESPACE @@ -91,6 +92,13 @@ public: static QByteArray hash(const QByteArray &data, Algorithm method); #endif static QByteArray hash(QByteArrayView data, Algorithm method); + + static QByteArrayView hashInto(QSpan buffer, QByteArrayView data, Algorithm method) noexcept + { return hashInto(as_writable_bytes(buffer), data, method); } + static QByteArrayView hashInto(QSpan buffer, QByteArrayView data, Algorithm method) noexcept + { return hashInto(as_writable_bytes(buffer), data, method); } + static QByteArrayView hashInto(QSpan buffer, QByteArrayView data, Algorithm method) noexcept; + static int hashLength(Algorithm method); static bool supportsAlgorithm(Algorithm method); private: diff --git a/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp b/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp index fe1a9b828af..47e0ead2708 100644 --- a/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp +++ b/tests/auto/corelib/tools/qcryptographichash/tst_qcryptographichash.cpp @@ -209,6 +209,9 @@ void tst_QCryptographicHash::static_hash() const auto _algo = QCryptographicHash::Algorithm(algo); QCOMPARE(QCryptographicHash::hash(first, _algo), hash_first); + + std::byte buffer[1024]; + QCOMPARE(QCryptographicHash::hashInto(buffer, first, _algo), hash_first); } @@ -511,6 +514,9 @@ void tst_QCryptographicHash::hashLength() expectedSize = 0; } else { expectedSize = QCryptographicHash::hash("test", algorithm).size(); + + std::byte buffer[1024]; + QCOMPARE(QCryptographicHash::hashInto(buffer, "foo", algorithm).size(), expectedSize); } QCOMPARE(QCryptographicHash::hashLength(algorithm), expectedSize); }