File: main.cpp

package info (click to toggle)
qtbase-opensource-src 5.15.15%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 350,700 kB
  • sloc: cpp: 2,089,913; ansic: 336,851; xml: 115,491; python: 9,447; java: 7,499; asm: 4,023; perl: 2,047; sh: 2,037; yacc: 1,687; lex: 1,333; javascript: 878; makefile: 273; objc: 70
file content (223 lines) | stat: -rw-r--r-- 6,281 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/****************************************************************************
**
** Copyright (C) 2023 The Qt Company Ltd.
** Copyright (C) 2017 Intel Corporation.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtCore module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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 General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** 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-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QByteArray>
#include <QCryptographicHash>
#include <QFile>
#include <QMetaEnum>
#include <QMessageAuthenticationCode>
#include <QRandomGenerator>
#include <QString>
#include <QtTest>

#include <functional>
#include <numeric>

#include <time.h>

class tst_bench_QCryptographicHash : public QObject
{
    Q_OBJECT
    QByteArray blockOfData;

    using Algorithm = QCryptographicHash::Algorithm;

public:
    tst_bench_QCryptographicHash();

private Q_SLOTS:
    void hash_data();
    void hash();
    void addData_data() { hash_data(); }
    void addData();
    void addDataChunked_data() { hash_data(); }
    void addDataChunked();

    // QMessageAuthenticationCode:
    void hmac_hash_data() { hash_data(); }
    void hmac_hash();
    void hmac_addData_data() { hash_data(); }
    void hmac_addData();
    void hmac_setKey_data();
    void hmac_setKey();
};

const int MaxBlockSize = 65536;

static void for_each_algorithm(std::function<void(QCryptographicHash::Algorithm, const char*)> f)
{
    Q_ASSERT(f);
    using A = QCryptographicHash::Algorithm;
    static const auto metaEnum = QMetaEnum::fromType<A>();
    for (int i = 0, value = metaEnum.value(i); value != -1; value = metaEnum.value(++i))
        f(A(value), metaEnum.key(i));
}

tst_bench_QCryptographicHash::tst_bench_QCryptographicHash()
    : blockOfData(MaxBlockSize, Qt::Uninitialized)
{
#ifdef Q_OS_UNIX
    QFile urandom("/dev/urandom");
    if (urandom.open(QIODevice::ReadOnly | QIODevice::Unbuffered)) {
        QCOMPARE(urandom.read(blockOfData.data(), blockOfData.size()), qint64(MaxBlockSize));
    } else
#endif
    {
        for (int i = 0; i < MaxBlockSize; ++i)
            blockOfData[i] = QRandomGenerator::global()->generate();
    }
}

void tst_bench_QCryptographicHash::hash_data()
{
    QTest::addColumn<Algorithm>("algo");
    QTest::addColumn<QByteArray>("data");

    static const int datasizes[] = { 0, 1, 64, 65, 512, 4095, 4096, 4097, 65536 };
    for (uint i = 0; i < sizeof(datasizes)/sizeof(datasizes[0]); ++i) {
        Q_ASSERT(datasizes[i] < MaxBlockSize);
        QByteArray data = QByteArray::fromRawData(blockOfData.constData(), datasizes[i]);

        for_each_algorithm([&] (Algorithm algo, const char *name) {
            QTest::addRow("%s-%d", name, datasizes[i]) << algo << data;
        });
    }
}

void tst_bench_QCryptographicHash::hash()
{
    QFETCH(const Algorithm, algo);
    QFETCH(QByteArray, data);

    QBENCHMARK {
        QCryptographicHash::hash(data, algo);
    }
}

void tst_bench_QCryptographicHash::addData()
{
    QFETCH(const Algorithm, algo);
    QFETCH(QByteArray, data);

    QCryptographicHash hash(algo);
    QBENCHMARK {
        hash.reset();
        hash.addData(data);
        hash.result();
    }
}

void tst_bench_QCryptographicHash::addDataChunked()
{
    QFETCH(const Algorithm, algo);
    QFETCH(QByteArray, data);

    QCryptographicHash hash(algo);
    QBENCHMARK {
        hash.reset();

        // add the data in chunks of 64 bytes
        for (int i = 0; i < data.size() / 64; ++i)
            hash.addData(data.constData() + 64 * i, 64);
        hash.addData(data.constData() + data.size() / 64 * 64, data.size() % 64);

        hash.result();
    }
}

static QByteArray hmacKey() {
    static QByteArray key = [] {
            QByteArray result(277, Qt::Uninitialized);
            std::iota(result.begin(), result.end(), uchar(0)); // uchar so wraps after UCHAR_MAX
            return result;
        }();
    return key;
}

void tst_bench_QCryptographicHash::hmac_hash()
{
    QFETCH(const Algorithm, algo);
    QFETCH(const QByteArray, data);

    const auto key = hmacKey();
    QBENCHMARK {
        auto r = QMessageAuthenticationCode::hash(data, key, algo);
        Q_UNUSED(r);
    }
}

void tst_bench_QCryptographicHash::hmac_addData()
{
    QFETCH(const Algorithm, algo);
    QFETCH(const QByteArray, data);

    const auto key = hmacKey();
    QMessageAuthenticationCode mac(algo, key);
    QBENCHMARK {
        mac.reset();
        mac.addData(data);
        auto r = mac.result();
        Q_UNUSED(r);
    }
}

void tst_bench_QCryptographicHash::hmac_setKey_data()
{
    QTest::addColumn<Algorithm>("algo");
    for_each_algorithm([] (Algorithm algo, const char *name) {
        QTest::addRow("%s", name) << algo;
    });
}

void tst_bench_QCryptographicHash::hmac_setKey()
{
    QFETCH(const Algorithm, algo);

    const QByteArrayList keys = [] {
            QByteArrayList result;
            const auto fullKey = hmacKey();
            result.reserve(fullKey.size());
            for (auto i = fullKey.size(); i > 0; --i)
                result.push_back(fullKey.mid(i));
            return result;
        }();

    QMessageAuthenticationCode mac(algo);
    QBENCHMARK {
        for (const auto &key : keys) {
            mac.setKey(key);
            mac.addData("abc", 3); // avoid lazy setKey()
        }
    }
}


QTEST_APPLESS_MAIN(tst_bench_QCryptographicHash)

#include "main.moc"