Use QCryptographicHash::hash() more widely

... instead of the "usual" rule of three: ctor, addData(), result().

Not only does it generate less code in the caller, it's now also
faster.

Change-Id: I67c7eeb01f527b90e80a08f60c1c7f2ec1e49dd4
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2021-07-06 08:12:00 +02:00
parent bd62dc1391
commit e9fffbaf81
3 changed files with 5 additions and 13 deletions

View File

@ -122,10 +122,7 @@ void QMessageAuthenticationCodePrivate::initMessageHash()
const int blockSize = qt_hash_block_size(method);
if (key.size() > blockSize) {
QCryptographicHash hash(method);
hash.addData(key);
key = hash.result();
hash.reset();
key = QCryptographicHash::hash(key, method);
}
if (key.size() < blockSize) {

View File

@ -594,10 +594,9 @@ QString QNetworkDiskCachePrivate::uniqueFileName(const QUrl &url)
cleanUrl.setPassword(QString());
cleanUrl.setFragment(QString());
QCryptographicHash hash(QCryptographicHash::Sha1);
hash.addData(cleanUrl.toEncoded());
const QByteArray hash = QCryptographicHash::hash(cleanUrl.toEncoded(), QCryptographicHash::Sha1);
// convert sha1 to base36 form and return first 8 bytes for use as string
const QByteArray id = QByteArray::number(*(qlonglong*)hash.result().constData(), 36).left(8);
const QByteArray id = QByteArray::number(*(qlonglong*)hash.data(), 36).left(8);
// generates <one-char subdir>/<8-char filname.d>
uint code = (uint)id.at(id.length()-1) % 16;
QString pathFragment = QString::number(code, 16) + QLatin1Char('/')

View File

@ -109,15 +109,11 @@ static QByteArray _q_PKCS12_keygen(char id, const QByteArray &salt, const QStrin
QByteArray A;
QByteArray B;
B.resize(v);
QCryptographicHash hash(QCryptographicHash::Sha1);
for (int i = 0; i < c; ++i) {
// hash r iterations
QByteArray Ai = D + I;
for (int j = 0; j < r; ++j) {
hash.reset();
hash.addData(Ai);
Ai = hash.result();
}
for (int j = 0; j < r; ++j)
Ai = QCryptographicHash::hash(Ai, QCryptographicHash::Sha1);
for (int j = 0; j < v; ++j)
B[j] = Ai[j % u];