1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | |
29 | |
30 | #include <QtCore/QCoreApplication> |
31 | #include <QtTest/QtTest> |
32 | #include <QtCore/QMetaEnum> |
33 | |
34 | Q_DECLARE_METATYPE(QCryptographicHash::Algorithm) |
35 | |
36 | class tst_QCryptographicHash : public QObject |
37 | { |
38 | Q_OBJECT |
39 | private slots: |
40 | void repeated_result_data(); |
41 | void repeated_result(); |
42 | void intermediary_result_data(); |
43 | void intermediary_result(); |
44 | void sha1(); |
45 | void sha3_data(); |
46 | void sha3(); |
47 | void files_data(); |
48 | void files(); |
49 | void hashLength(); |
50 | }; |
51 | |
52 | void tst_QCryptographicHash::repeated_result_data() |
53 | { |
54 | intermediary_result_data(); |
55 | } |
56 | |
57 | void tst_QCryptographicHash::repeated_result() |
58 | { |
59 | QFETCH(int, algo); |
60 | QCryptographicHash::Algorithm _algo = QCryptographicHash::Algorithm(algo); |
61 | QCryptographicHash hash(_algo); |
62 | |
63 | QFETCH(QByteArray, first); |
64 | hash.addData(data: first); |
65 | |
66 | QFETCH(QByteArray, hash_first); |
67 | QByteArray result = hash.result(); |
68 | QCOMPARE(result, hash_first); |
69 | QCOMPARE(result, hash.result()); |
70 | |
71 | hash.reset(); |
72 | hash.addData(data: first); |
73 | result = hash.result(); |
74 | QCOMPARE(result, hash_first); |
75 | QCOMPARE(result, hash.result()); |
76 | } |
77 | |
78 | void tst_QCryptographicHash::intermediary_result_data() |
79 | { |
80 | QTest::addColumn<int>(name: "algo" ); |
81 | QTest::addColumn<QByteArray>(name: "first" ); |
82 | QTest::addColumn<QByteArray>(name: "second" ); |
83 | QTest::addColumn<QByteArray>(name: "hash_first" ); |
84 | QTest::addColumn<QByteArray>(name: "hash_firstsecond" ); |
85 | |
86 | QTest::newRow(dataTag: "md4" ) << int(QCryptographicHash::Md4) |
87 | << QByteArray("abc" ) << QByteArray("abc" ) |
88 | << QByteArray::fromHex(hexEncoded: "A448017AAF21D8525FC10AE87AA6729D" ) |
89 | << QByteArray::fromHex(hexEncoded: "03E5E436DAFAF3B9B3589DB83C417C6B" ); |
90 | QTest::newRow(dataTag: "md5" ) << int(QCryptographicHash::Md5) |
91 | << QByteArray("abc" ) << QByteArray("abc" ) |
92 | << QByteArray::fromHex(hexEncoded: "900150983CD24FB0D6963F7D28E17F72" ) |
93 | << QByteArray::fromHex(hexEncoded: "440AC85892CA43AD26D44C7AD9D47D3E" ); |
94 | QTest::newRow(dataTag: "sha1" ) << int(QCryptographicHash::Sha1) |
95 | << QByteArray("abc" ) << QByteArray("abc" ) |
96 | << QByteArray::fromHex(hexEncoded: "A9993E364706816ABA3E25717850C26C9CD0D89D" ) |
97 | << QByteArray::fromHex(hexEncoded: "F8C1D87006FBF7E5CC4B026C3138BC046883DC71" ); |
98 | QTest::newRow(dataTag: "sha224" ) << int(QCryptographicHash::Sha224) |
99 | << QByteArray("abc" ) << QByteArray("abc" ) |
100 | << QByteArray::fromHex(hexEncoded: "23097D223405D8228642A477BDA255B32AADBCE4BDA0B3F7E36C9DA7" ) |
101 | << QByteArray::fromHex(hexEncoded: "7C9C91FC479626AA1A525301084DEB96716131D146A2DB61B533F4C9" ); |
102 | QTest::newRow(dataTag: "sha256" ) << int(QCryptographicHash::Sha256) |
103 | << QByteArray("abc" ) << QByteArray("abc" ) |
104 | << QByteArray::fromHex(hexEncoded: "BA7816BF8F01CFEA414140DE5DAE2223B00361A396177A9CB410FF61F20015AD" ) |
105 | << QByteArray::fromHex(hexEncoded: "BBB59DA3AF939F7AF5F360F2CEB80A496E3BAE1CD87DDE426DB0AE40677E1C2C" ); |
106 | QTest::newRow(dataTag: "sha384" ) << int(QCryptographicHash::Sha384) |
107 | << QByteArray("abc" ) << QByteArray("abc" ) |
108 | << QByteArray::fromHex(hexEncoded: "CB00753F45A35E8BB5A03D699AC65007272C32AB0EDED1631A8B605A43FF5BED8086072BA1E7CC2358BAECA134C825A7" ) |
109 | << QByteArray::fromHex(hexEncoded: "CAF33A735C9535CE7F5D24FB5B3A4834F0E9316664AD15A9E8221679D4A3B4FB7E962404BA0C10C1D43AB49D03A08B8D" ); |
110 | QTest::newRow(dataTag: "sha512" ) << int(QCryptographicHash::Sha512) |
111 | << QByteArray("abc" ) << QByteArray("abc" ) |
112 | << QByteArray::fromHex(hexEncoded: "DDAF35A193617ABACC417349AE20413112E6FA4E89A97EA20A9EEEE64B55D39A2192992A274FC1A836BA3C23A3FEEBBD454D4423643CE80E2A9AC94FA54CA49F" ) |
113 | << QByteArray::fromHex(hexEncoded: "F3C41E7B63EE869596FC28BAD64120612C520F65928AB4D126C72C6998B551B8FF1CEDDFED4373E6717554DC89D1EEE6F0AB22FD3675E561ABA9AE26A3EEC53B" ); |
114 | |
115 | QTest::newRow(dataTag: "sha3_224_empty_abc" ) |
116 | << int(QCryptographicHash::Sha3_224) |
117 | << QByteArray("" ) << QByteArray("abc" ) |
118 | << QByteArray::fromHex(hexEncoded: "6B4E03423667DBB73B6E15454F0EB1ABD4597F9A1B078E3F5B5A6BC7" ) |
119 | << QByteArray::fromHex(hexEncoded: "E642824C3F8CF24AD09234EE7D3C766FC9A3A5168D0C94AD73B46FDF" ); |
120 | QTest::newRow(dataTag: "sha3_256_empty_abc" ) |
121 | << int(QCryptographicHash::Sha3_256) |
122 | << QByteArray("" ) << QByteArray("abc" ) |
123 | << QByteArray::fromHex(hexEncoded: "A7FFC6F8BF1ED76651C14756A061D662F580FF4DE43B49FA82D80A4B80F8434A" ) |
124 | << QByteArray::fromHex(hexEncoded: "3A985DA74FE225B2045C172D6BD390BD855F086E3E9D525B46BFE24511431532" ); |
125 | QTest::newRow(dataTag: "sha3_384_empty_abc" ) |
126 | << int(QCryptographicHash::Sha3_384) |
127 | << QByteArray("" ) << QByteArray("abc" ) |
128 | << QByteArray::fromHex(hexEncoded: "0C63A75B845E4F7D01107D852E4C2485C51A50AAAA94FC61995E71BBEE983A2AC3713831264ADB47FB6BD1E058D5F004" ) |
129 | << QByteArray::fromHex(hexEncoded: "EC01498288516FC926459F58E2C6AD8DF9B473CB0FC08C2596DA7CF0E49BE4B298D88CEA927AC7F539F1EDF228376D25" ); |
130 | QTest::newRow(dataTag: "sha3_512_empty_abc" ) |
131 | << int(QCryptographicHash::Sha3_512) |
132 | << QByteArray("" ) << QByteArray("abc" ) |
133 | << QByteArray::fromHex(hexEncoded: "A69F73CCA23A9AC5C8B567DC185A756E97C982164FE25859E0D1DCC1475C80A615B2123AF1F5F94C11E3E9402C3AC558F500199D95B6D3E301758586281DCD26" ) |
134 | << QByteArray::fromHex(hexEncoded: "B751850B1A57168A5693CD924B6B096E08F621827444F70D884F5D0240D2712E10E116E9192AF3C91A7EC57647E3934057340B4CF408D5A56592F8274EEC53F0" ); |
135 | |
136 | QTest::newRow(dataTag: "sha3_224_abc_abc" ) |
137 | << int(QCryptographicHash::Sha3_224) |
138 | << QByteArray("abc" ) << QByteArray("abc" ) |
139 | << QByteArray::fromHex(hexEncoded: "E642824C3F8CF24AD09234EE7D3C766FC9A3A5168D0C94AD73B46FDF" ) |
140 | << QByteArray::fromHex(hexEncoded: "58F426458091E16FBC61DDCB8F2D2A6F30F729CAFA3C289A4EB2BCF8" ); |
141 | QTest::newRow(dataTag: "sha3_256_abc_abc" ) |
142 | << int(QCryptographicHash::Sha3_256) |
143 | << QByteArray("abc" ) << QByteArray("abc" ) |
144 | << QByteArray::fromHex(hexEncoded: "3A985DA74FE225B2045C172D6BD390BD855F086E3E9D525B46BFE24511431532" ) |
145 | << QByteArray::fromHex(hexEncoded: "6C0872716337DE1EE664C1E37F64ADE109448F02681C63A912BC230FDEFC0058" ); |
146 | QTest::newRow(dataTag: "sha3_384_abc_abc" ) |
147 | << int(QCryptographicHash::Sha3_384) |
148 | << QByteArray("abc" ) << QByteArray("abc" ) |
149 | << QByteArray::fromHex(hexEncoded: "EC01498288516FC926459F58E2C6AD8DF9B473CB0FC08C2596DA7CF0E49BE4B298D88CEA927AC7F539F1EDF228376D25" ) |
150 | << QByteArray::fromHex(hexEncoded: "34FA93E11E467D610524EC91CEDC848EE1395BCF8F4F987455478E63DB0BCE47194D33D1251A3CC32BBB18D8726040D0" ); |
151 | QTest::newRow(dataTag: "sha3_512_abc_abc" ) |
152 | << int(QCryptographicHash::Sha3_512) |
153 | << QByteArray("abc" ) << QByteArray("abc" ) |
154 | << QByteArray::fromHex(hexEncoded: "B751850B1A57168A5693CD924B6B096E08F621827444F70D884F5D0240D2712E10E116E9192AF3C91A7EC57647E3934057340B4CF408D5A56592F8274EEC53F0" ) |
155 | << QByteArray::fromHex(hexEncoded: "BB582DA40D15399ACF62AFCBBD6CFC9EE1DD5129B1EF9935DD3B21668F1A73D7841018BE3B13F281C3A8E9DA7EDB60F57B9F9F1C04033DF4CE3654B7B2ADB310" ); |
156 | } |
157 | |
158 | void tst_QCryptographicHash::intermediary_result() |
159 | { |
160 | QFETCH(int, algo); |
161 | QCryptographicHash::Algorithm _algo = QCryptographicHash::Algorithm(algo); |
162 | QCryptographicHash hash(_algo); |
163 | |
164 | QFETCH(QByteArray, first); |
165 | hash.addData(data: first); |
166 | |
167 | QFETCH(QByteArray, hash_first); |
168 | QByteArray result = hash.result(); |
169 | QCOMPARE(result, hash_first); |
170 | |
171 | // don't reset |
172 | QFETCH(QByteArray, second); |
173 | QFETCH(QByteArray, hash_firstsecond); |
174 | hash.addData(data: second); |
175 | |
176 | result = hash.result(); |
177 | QCOMPARE(result, hash_firstsecond); |
178 | |
179 | hash.reset(); |
180 | } |
181 | |
182 | |
183 | void tst_QCryptographicHash::sha1() |
184 | { |
185 | // SHA1("abc") = |
186 | // A9993E36 4706816A BA3E2571 7850C26C 9CD0D89D |
187 | QCOMPARE(QCryptographicHash::hash("abc" , QCryptographicHash::Sha1).toHex().toUpper(), |
188 | QByteArray("A9993E364706816ABA3E25717850C26C9CD0D89D" )); |
189 | |
190 | // SHA1("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq") = |
191 | // 84983E44 1C3BD26E BAAE4AA1 F95129E5 E54670F1 |
192 | QCOMPARE(QCryptographicHash::hash("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" , |
193 | QCryptographicHash::Sha1).toHex().toUpper(), |
194 | QByteArray("84983E441C3BD26EBAAE4AA1F95129E5E54670F1" )); |
195 | |
196 | // SHA1(A million repetitions of "a") = |
197 | // 34AA973C D4C4DAA4 F61EEB2B DBAD2731 6534016F |
198 | QByteArray as; |
199 | for (int i = 0; i < 1000000; ++i) |
200 | as += 'a'; |
201 | QCOMPARE(QCryptographicHash::hash(as, QCryptographicHash::Sha1).toHex().toUpper(), |
202 | QByteArray("34AA973CD4C4DAA4F61EEB2BDBAD27316534016F" )); |
203 | } |
204 | |
205 | void tst_QCryptographicHash::sha3_data() |
206 | { |
207 | QTest::addColumn<QCryptographicHash::Algorithm>(name: "algorithm" ); |
208 | QTest::addColumn<QByteArray>(name: "data" ); |
209 | QTest::addColumn<QByteArray>(name: "expectedResult" ); |
210 | |
211 | #define ROW(Tag, Algorithm, Input, Result) \ |
212 | QTest::newRow(Tag) << Algorithm << QByteArrayLiteral(Input) << QByteArray::fromHex(Result) |
213 | |
214 | ROW("sha3_224_pangram" , |
215 | QCryptographicHash::Sha3_224, |
216 | "The quick brown fox jumps over the lazy dog" , |
217 | "d15dadceaa4d5d7bb3b48f446421d542e08ad8887305e28d58335795" ); |
218 | |
219 | ROW("sha3_224_pangram_dot" , |
220 | QCryptographicHash::Sha3_224, |
221 | "The quick brown fox jumps over the lazy dog." , |
222 | "2d0708903833afabdd232a20201176e8b58c5be8a6fe74265ac54db0" ); |
223 | |
224 | ROW("sha3_256_pangram" , |
225 | QCryptographicHash::Sha3_256, |
226 | "The quick brown fox jumps over the lazy dog" , |
227 | "69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04" ); |
228 | |
229 | ROW("sha3_256_pangram_dot" , |
230 | QCryptographicHash::Sha3_256, |
231 | "The quick brown fox jumps over the lazy dog." , |
232 | "a80f839cd4f83f6c3dafc87feae470045e4eb0d366397d5c6ce34ba1739f734d" ); |
233 | |
234 | ROW("sha3_384_pangram" , |
235 | QCryptographicHash::Sha3_384, |
236 | "The quick brown fox jumps over the lazy dog" , |
237 | "7063465e08a93bce31cd89d2e3ca8f602498696e253592ed26f07bf7e703cf328581e1471a7ba7ab119b1a9ebdf8be41" ); |
238 | |
239 | ROW("sha3_384_pangram_dot" , |
240 | QCryptographicHash::Sha3_384, |
241 | "The quick brown fox jumps over the lazy dog." , |
242 | "1a34d81695b622df178bc74df7124fe12fac0f64ba5250b78b99c1273d4b080168e10652894ecad5f1f4d5b965437fb9" ); |
243 | |
244 | ROW("sha3_512_pangram" , |
245 | QCryptographicHash::Sha3_512, |
246 | "The quick brown fox jumps over the lazy dog" , |
247 | "01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450" ); |
248 | |
249 | ROW("sha3_512_pangram_dot" , |
250 | QCryptographicHash::Sha3_512, |
251 | "The quick brown fox jumps over the lazy dog." , |
252 | "18f4f4bd419603f95538837003d9d254c26c23765565162247483f65c50303597bc9ce4d289f21d1c2f1f458828e33dc442100331b35e7eb031b5d38ba6460f8" ); |
253 | |
254 | #undef ROW |
255 | } |
256 | |
257 | void tst_QCryptographicHash::sha3() |
258 | { |
259 | QFETCH(QCryptographicHash::Algorithm, algorithm); |
260 | QFETCH(QByteArray, data); |
261 | QFETCH(QByteArray, expectedResult); |
262 | |
263 | const auto result = QCryptographicHash::hash(data, method: algorithm); |
264 | QCOMPARE(result, expectedResult); |
265 | } |
266 | |
267 | void tst_QCryptographicHash::files_data() { |
268 | QTest::addColumn<QString>(name: "filename" ); |
269 | QTest::addColumn<QCryptographicHash::Algorithm>(name: "algorithm" ); |
270 | QTest::addColumn<QByteArray>(name: "md5sum" ); |
271 | QTest::newRow(dataTag: "data1" ) << QString::fromLatin1(str: "data/2c1517dad3678f03917f15849b052fd5.md5" ) << QCryptographicHash::Md5 << QByteArray("2c1517dad3678f03917f15849b052fd5" ); |
272 | QTest::newRow(dataTag: "data2" ) << QString::fromLatin1(str: "data/d41d8cd98f00b204e9800998ecf8427e.md5" ) << QCryptographicHash::Md5 << QByteArray("d41d8cd98f00b204e9800998ecf8427e" ); |
273 | } |
274 | |
275 | |
276 | void tst_QCryptographicHash::files() |
277 | { |
278 | QFETCH(QString, filename); |
279 | QFETCH(QCryptographicHash::Algorithm, algorithm); |
280 | QFETCH(QByteArray, md5sum); |
281 | { |
282 | QString testData = QFINDTESTDATA(filename); |
283 | QVERIFY2(!testData.isEmpty(), qPrintable(QString("Cannot find test data: %1" ).arg(filename))); |
284 | QFile f(testData); |
285 | QCryptographicHash hash(algorithm); |
286 | QVERIFY(! hash.addData(&f)); // file is not open for reading; |
287 | if (f.open(flags: QIODevice::ReadOnly)) { |
288 | QVERIFY(hash.addData(&f)); |
289 | QCOMPARE(hash.result().toHex(),md5sum); |
290 | } else { |
291 | QFAIL("Failed to open file for testing. should not happen" ); |
292 | } |
293 | } |
294 | } |
295 | |
296 | void tst_QCryptographicHash::hashLength() |
297 | { |
298 | auto metaEnum = QMetaEnum::fromType<QCryptographicHash::Algorithm>(); |
299 | for (int i = 0, value = metaEnum.value(index: i); value != -1; value = metaEnum.value(index: ++i)) { |
300 | auto algorithm = QCryptographicHash::Algorithm(value); |
301 | QByteArray output = QCryptographicHash::hash(QByteArrayLiteral("test" ), method: algorithm); |
302 | QCOMPARE(QCryptographicHash::hashLength(algorithm), output.length()); |
303 | } |
304 | } |
305 | |
306 | QTEST_MAIN(tst_QCryptographicHash) |
307 | #include "tst_qcryptographichash.moc" |
308 | |