QCryptographicHash: port addData() to QByteArrayView

Incl. the static hash() function. Remove the QByteArray versions from
the API, but not the ABI.

Adapt some callers.

[ChangeLog][QtCore][QCryptographicHash] Replaced QByteArray with
QByteArrayView in addData() and static hash() functions.

Change-Id: Ia0e9bf726276305e05894d323d76a29e985f39eb
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2021-07-05 20:10:07 +02:00
parent 85831bcfe6
commit de18b3ff37
7 changed files with 128 additions and 33 deletions

View File

@ -241,6 +241,7 @@ qt_internal_add_module(Core
tools/qsize.cpp tools/qsize.h
tools/qstack.h
tools/qtaggedpointer.h
tools/qtcore_tools_removed_functions_in_6_3.cpp
tools/qtools_p.h
tools/qvarlengtharray.h
tools/qvector.h
@ -271,7 +272,14 @@ qt_internal_add_module(Core
# special case end
)
qt_update_ignore_pch_source(Core kernel/qmetatype.cpp )
set(corelib_no_pch_sources
kernel/qmetatype.cpp
tools/qtcore_tools_removed_functions_in_6_3.cpp
)
foreach(src ${corelib_no_pch_sources})
qt_update_ignore_pch_source(Core ${src})
endforeach()
# special case begin
add_dependencies(Core qmodule_pri)

View File

@ -351,13 +351,32 @@ void QCryptographicHash::reset()
d->result.clear();
}
#if QT_DEPRECATED_SINCE(6, 4)
/*!
Adds the first \a length chars of \a data to the cryptographic
hash.
\obsolete
Use the QByteArrayView overload instead.
*/
void QCryptographicHash::addData(const char *data, qsizetype length)
{
Q_ASSERT(length >= 0);
addData(QByteArrayView{data, length});
}
#endif
/*!
Adds the first \a length chars of \a data to the cryptographic
hash.
\note In Qt versions prior to 6.3, this function took QByteArray,
not QByteArrayView.
*/
void QCryptographicHash::addData(QByteArrayView bytes) noexcept
{
const char *data = bytes.data();
auto length = bytes.size();
#if QT_POINTER_SIZE == 8
// feed the data UINT_MAX bytes at a time, as some of the methods below
@ -430,14 +449,6 @@ void QCryptographicHash::addData(const char *data, qsizetype length)
d->result.clear();
}
/*!
\overload addData()
*/
void QCryptographicHash::addData(const QByteArray &data)
{
addData(data.constData(), data.length());
}
/*!
Reads the data from the open QIODevice \a device until it ends
and hashes it. Returns \c true if reading was successful.
@ -455,7 +466,7 @@ bool QCryptographicHash::addData(QIODevice *device)
int length;
while ((length = device->read(buffer, sizeof(buffer))) > 0)
addData(buffer, length);
addData({buffer, length});
return device->atEnd();
}
@ -580,8 +591,11 @@ QByteArray QCryptographicHash::result() const
/*!
Returns the hash of \a data using \a method.
\note In Qt versions prior to 6.3, this function took QByteArray,
not QByteArrayView.
*/
QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
QByteArray QCryptographicHash::hash(QByteArrayView data, Algorithm method)
{
QCryptographicHash hash(method);
hash.addData(data);

View File

@ -1,5 +1,6 @@
/****************************************************************************
**
** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
** Copyright (C) 2016 The Qt Company Ltd.
** Copyright (C) 2013 Richard J. Moore <rich@kde.org>.
** Contact: https://www.qt.io/licensing/
@ -103,13 +104,22 @@ public:
void reset();
#if QT_DEPRECATED_SINCE(6, 4)
QT_DEPRECATED_VERSION_X_6_4("Use the QByteArrayView overload instead")
void addData(const char *data, qsizetype length);
#endif
#ifdef QT_BUILD_FUNCTIONS_REMOVED_IN_6_3
void addData(const QByteArray &data);
#endif
void addData(QByteArrayView data) noexcept;
bool addData(QIODevice *device);
QByteArray result() const;
#ifdef QT_BUILD_FUNCTIONS_REMOVED_IN_6_3
static QByteArray hash(const QByteArray &data, Algorithm method);
#endif
static QByteArray hash(QByteArrayView data, Algorithm method);
static int hashLength(Algorithm method);
private:
Q_DISABLE_COPY(QCryptographicHash)

View File

@ -140,7 +140,7 @@ void QMessageAuthenticationCodePrivate::initMessageHash()
for (int i = 0; i < blockSize; ++i)
iKeyPad[i] = keyData[i] ^ 0x36;
messageHash.addData(iKeyPad.data(), iKeyPad.size());
messageHash.addData(iKeyPad);
}
/*!
@ -216,7 +216,7 @@ void QMessageAuthenticationCode::setKey(const QByteArray &key)
void QMessageAuthenticationCode::addData(const char *data, qsizetype length)
{
d->initMessageHash();
d->messageHash.addData(data, length);
d->messageHash.addData({data, length});
}
/*!
@ -263,7 +263,7 @@ QByteArray QMessageAuthenticationCode::result() const
oKeyPad[i] = keyData[i] ^ 0x5c;
QCryptographicHash hash(d->method);
hash.addData(oKeyPad.data(), oKeyPad.size());
hash.addData(oKeyPad);
hash.addData(hashedMessage);
d->result = hash.result();

View File

@ -0,0 +1,63 @@
/****************************************************************************
**
** Copyright (C) 2021 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com, author Marc Mutz <marc.mutz@kdab.com>
** Contact: http://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
#define QT_BUILD_FUNCTIONS_REMOVED_IN_6_3
#include "qglobal.h"
QT_USE_NAMESPACE
#if QT_DEPRECATED_SINCE(6, 3)
#include "tools/qcryptographichash.h"
void QCryptographicHash::addData(const QByteArray &data)
{
addData(QByteArrayView{data});
}
QByteArray QCryptographicHash::hash(const QByteArray &data, Algorithm method)
{
return hash(QByteArrayView{data}, method);
}
// #include <qotherheader.h>
// // implement removed functions from qotherheader.h
#endif // QT_DEPRECATED_SINCE(6, 3)

View File

@ -732,9 +732,9 @@ static QByteArray digestMd5ResponseHelper(
{
QCryptographicHash hash(QCryptographicHash::Md5);
hash.addData(userName);
hash.addData(":", 1);
hash.addData(":");
hash.addData(realm);
hash.addData(":", 1);
hash.addData(":");
hash.addData(password);
QByteArray ha1 = hash.result();
if (alg.compare("md5-sess", Qt::CaseInsensitive) == 0) {
@ -744,9 +744,9 @@ static QByteArray digestMd5ResponseHelper(
// but according to the errata page at http://www.rfc-editor.org/errata_list.php, ID 1649, it
// must be the following line:
hash.addData(ha1.toHex());
hash.addData(":", 1);
hash.addData(":");
hash.addData(nonce);
hash.addData(":", 1);
hash.addData(":");
hash.addData(cNonce);
ha1 = hash.result();
};
@ -755,10 +755,10 @@ static QByteArray digestMd5ResponseHelper(
// calculate H(A2)
hash.reset();
hash.addData(method);
hash.addData(":", 1);
hash.addData(":");
hash.addData(digestUri);
if (qop.compare("auth-int", Qt::CaseInsensitive) == 0) {
hash.addData(":", 1);
hash.addData(":");
hash.addData(hEntity);
}
QByteArray ha2hex = hash.result().toHex();
@ -766,16 +766,16 @@ static QByteArray digestMd5ResponseHelper(
// calculate response
hash.reset();
hash.addData(ha1);
hash.addData(":", 1);
hash.addData(":");
hash.addData(nonce);
hash.addData(":", 1);
hash.addData(":");
if (!qop.isNull()) {
hash.addData(nonceCount);
hash.addData(":", 1);
hash.addData(":");
hash.addData(cNonce);
hash.addData(":", 1);
hash.addData(":");
hash.addData(qop);
hash.addData(":", 1);
hash.addData(":");
}
hash.addData(ha2hex);
return hash.result().toHex();
@ -1302,7 +1302,7 @@ static QByteArray qCreatev2Hash(const QAuthenticatorPrivate *ctx,
if (phase3->v2Hash.size() == 0) {
QCryptographicHash md4(QCryptographicHash::Md4);
QByteArray passUnicode = qStringAsUcs2Le(ctx->password);
md4.addData(passUnicode.data(), passUnicode.size());
md4.addData(passUnicode);
QByteArray hashKey = md4.result();
Q_ASSERT(hashKey.size() == 16);

View File

@ -425,7 +425,7 @@ void tst_QCryptographicHash::hashLength()
{
QFETCH(const QCryptographicHash::Algorithm, algorithm);
QByteArray output = QCryptographicHash::hash(QByteArrayLiteral("test"), algorithm);
QByteArray output = QCryptographicHash::hash("test", algorithm);
QCOMPARE(QCryptographicHash::hashLength(algorithm), output.length());
}
@ -480,22 +480,22 @@ void tst_QCryptographicHash::moreThan4GiBOfData()
qDebug() << algorithm << "test finished in" << timer.restart() << "ms";
});
const auto begin = large.data();
const auto mid = begin + large.size() / 2;
const auto end = begin + large.size();
const auto view = QByteArrayView{large};
const auto first = view.first(view.size() / 2);
const auto last = view.sliced(view.size() / 2);
QByteArray single;
QByteArray chunked;
auto t = MaybeThread{[&] {
QCryptographicHash h(algorithm);
h.addData(begin, end - begin);
h.addData(view);
single = h.result();
}};
{
QCryptographicHash h(algorithm);
h.addData(begin, mid - begin);
h.addData(mid, end - mid);
h.addData(first);
h.addData(last);
chunked = h.result();
}
t.join();