File: hashunittest.cpp

package info (click to toggle)
qca2 2.3.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,672 kB
  • sloc: cpp: 58,980; ansic: 832; perl: 133; sh: 79; makefile: 22
file content (861 lines) | stat: -rw-r--r-- 36,090 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/**
 * Copyright (C)  2004-2006  Brad Hards <bradh@frogmouth.net>
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in the
 *   documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <QFile>
#include <QtCrypto>
#include <QtTest/QtTest>

#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif

class HashUnitTest : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();
    void cleanupTestCase();
    void md2test_data();
    void md2test();
    void md4test_data();
    void md4test();
    void md5test_data();
    void md5test();
    void md5filetest();
    void sha0test_data();
    void sha0test();
    void sha0longtest();
    void sha1test_data();
    void sha1test();
    void sha1longtest();
    void sha224test_data();
    void sha224test();
    void sha224longtest();
    void sha256test_data();
    void sha256test();
    void sha256longtest();
    void sha384test_data();
    void sha384test();
    void sha384longtest();
    void sha512test_data();
    void sha512test();
    void sha512longtest();
    void rmd160test_data();
    void rmd160test();
    void rmd160longtest();
    void whirlpooltest_data();
    void whirlpooltest();
    void whirlpoollongtest();

private:
    QCA::Initializer *m_init;
    QStringList       providersToTest;
};

void HashUnitTest::initTestCase()
{
    m_init               = new QCA::Initializer;
    const auto providers = QCA::providers();
    for (QCA::Provider *provider : providers)
        providersToTest << provider->name();
}

void HashUnitTest::cleanupTestCase()
{
    QCA::unloadAllPlugins();
    delete m_init;
}

void HashUnitTest::md2test_data()
{
    // These are as specified in RFC 1319
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    QTest::newRow("md2()") << QByteArray("") << QStringLiteral("8350e5a3e24c153df2275c9f80692773");
    QTest::newRow("md2(a)") << QByteArray("a") << QStringLiteral("32ec01ec4a6dac72c0ab96fb34c0b5d1");
    QTest::newRow("md2(abc)") << QByteArray("abc") << QStringLiteral("da853b0d3f88d99b30283a69e6ded6bb");
    QTest::newRow("md2(messageDigest)") << QByteArray("message digest")
                                        << QStringLiteral("ab4f496bfb2a530b219ff33031fe06b0");
    QTest::newRow("md2([a-z])") << QByteArray("abcdefghijklmnopqrstuvwxyz")
                                << QStringLiteral("4e8ddff3650292ab5a4108c3aa47940b");
    QTest::newRow("md2([A-z,0-9])") << QByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
                                    << QStringLiteral("da33def2a42df13975352846c30338cd");
    QTest::newRow("md2(nums)")
        << QByteArray("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
        << QStringLiteral("d5976f79d83d3a0dc9806c3c66f3efd8");
}

void HashUnitTest::md2test()
{
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    bool anyProviderTested = false;
    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("md2", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("md2"), provider);
            QCA::Hash copy = hash;
            copy.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports MD2:" << providersToTest;
}

void HashUnitTest::md4test_data()
{
    // These are as specified in RFC 1320
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    QTest::newRow("md4()") << QByteArray("") << QStringLiteral("31d6cfe0d16ae931b73c59d7e0c089c0");
    QTest::newRow("md4(a)") << QByteArray("a") << QStringLiteral("bde52cb31de33e46245e05fbdbd6fb24");
    QTest::newRow("md4(abc)") << QByteArray("abc") << QStringLiteral("a448017aaf21d8525fc10ae87aa6729d");
    QTest::newRow("md4(messageDigest)") << QByteArray("message digest")
                                        << QStringLiteral("d9130a8164549fe818874806e1c7014b");
    QTest::newRow("md4([a-z])") << QByteArray("abcdefghijklmnopqrstuvwxyz")
                                << QStringLiteral("d79e1c308aa5bbcdeea8ed63df412da9");
    QTest::newRow("md4([A-z,0-9])") << QByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
                                    << QStringLiteral("043f8582f241db351ce627e153e7f0e4");
    QTest::newRow("md4(nums)")
        << QByteArray("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
        << QStringLiteral("e33b4ddc9c38f2199c3e7b164fcc0536");
}

void HashUnitTest::md4test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("md4", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("md4"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports MD4:" << providersToTest;
}

void HashUnitTest::md5test_data()
{
    // These are as specified in RFC 1321
    // They also match Australian Standard (AS) 2805.1.3.2-2000 Appendix A
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    QTest::newRow("md5()") << QByteArray("") << QStringLiteral("d41d8cd98f00b204e9800998ecf8427e");
    QTest::newRow("md5(a)") << QByteArray("a") << QStringLiteral("0cc175b9c0f1b6a831c399e269772661");
    QTest::newRow("md5(abc)") << QByteArray("abc") << QStringLiteral("900150983cd24fb0d6963f7d28e17f72");
    QTest::newRow("md5(messageDigest)") << QByteArray("message digest")
                                        << QStringLiteral("f96b697d7cb7938d525a2f31aaf161d0");
    QTest::newRow("md5([a-z])") << QByteArray("abcdefghijklmnopqrstuvwxyz")
                                << QStringLiteral("c3fcd3d76192e4007dfb496cca67e13b");
    QTest::newRow("md5([A-z,0-9])") << QByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
                                    << QStringLiteral("d174ab98d277d9f5a5611c2c9f419d9f");
    QTest::newRow("md5(nums)")
        << QByteArray("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
        << QStringLiteral("57edf4a22be3c955ac49da2e2107b67a");
}

void HashUnitTest::md5test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("md5", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("md5"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports MD2:" << providersToTest;
}

void HashUnitTest::md5filetest()
{
    foreach (QString provider, providersToTest) {
        if (!QCA::isSupported("md5", provider)) {
            QFile f1(QStringLiteral(TEST_DATA_DIR "/data/empty"));
            QVERIFY(f1.open(QIODevice::ReadOnly));
            {
                QCA::Hash hashObj(QStringLiteral("md5"), provider);
                hashObj.update(&f1);
                QCOMPARE(QString(QCA::arrayToHex(hashObj.final().toByteArray())),
                         QStringLiteral("d41d8cd98f00b204e9800998ecf8427e"));
            }

            QFile f2(QStringLiteral(TEST_DATA_DIR "/data/twobytes"));
            QVERIFY(f2.open(QIODevice::ReadOnly));
            {
                QCA::Hash hashObj(QStringLiteral("md5"), provider);
                hashObj.update(&f2);
                QCOMPARE(QString(QCA::arrayToHex(hashObj.final().toByteArray())),
                         QStringLiteral("5fc9808ed18e442ab4164c59f151e757"));
            }

            QFile f3(QStringLiteral(TEST_DATA_DIR "/data/twohundredbytes"));
            QVERIFY(f3.open(QIODevice::ReadOnly));
            {
                QCA::Hash hashObj(QStringLiteral("md5"), provider);
                hashObj.update(&f3);
                QCOMPARE(QString(QCA::arrayToHex(hashObj.final().toByteArray())),
                         QStringLiteral("b91c1f114d942520ecdf7e84e580cda3"));
            }
        }
    }
}

void HashUnitTest::sha0test_data()
{
    // These are extracted from OpenOffice.org 1.1.2, in sal/workben/t_digest.c
    // Check FIPS 180-1?
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    QTest::newRow("sha0(abc)") << QByteArray("abc") << QStringLiteral("0164b8a914cd2a5e74c4f7ff082c4d97f1edf880");
    QTest::newRow("sha0(abc)") << QByteArray("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
                               << QStringLiteral("d2516ee1acfa5baf33dfc1c471e438449ef134c8");
}

void HashUnitTest::sha0test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha0", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("sha0"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports SHA0:" << providersToTest;
}

void HashUnitTest::sha0longtest()
{
    QByteArray fillerString;
    fillerString.fill('a', 1000);

    // This test extracted from OpenOffice.org 1.1.2, in sal/workben/t_digest.c

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha0", provider)) {
            QCA::Hash shaHash(QStringLiteral("sha0"), provider);
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("3232affa48628a26653b5aaa44541fd90d690603"));

            shaHash.clear();
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("3232affa48628a26653b5aaa44541fd90d690603"));
        }
    }
}

void HashUnitTest::sha1test_data()
{
    // These are as specified in FIPS 180-2. Matches RFC3174
    // Some additions from Australian Standard (AS) 2805.13.3-2000
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    // FIPS 180-2, Appendix A.1
    QTest::newRow("sha1(abc)") << QByteArray("abc") << QStringLiteral("a9993e364706816aba3e25717850c26c9cd0d89d");

    // FIPS 180-2, Appendix A.2
    QTest::newRow("sha1(a-q)") << QByteArray("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
                               << QStringLiteral("84983e441c3bd26ebaae4aa1f95129e5e54670f1");

    // AS 2805.13.3-200 Appendix A
    // also has some duplicates from FIPS 180-2
    QTest::newRow("sha1()") << QByteArray("") << QStringLiteral("da39a3ee5e6b4b0d3255bfef95601890afd80709");
    QTest::newRow("sha1(a)") << QByteArray("a") << QStringLiteral("86f7e437faa5a7fce15d1ddcb9eaeaea377667b8");
    QTest::newRow("sha1(a-z)") << QByteArray("abcdefghijklmnopqrstuvwxyz")
                               << QStringLiteral("32d10c7b8cf96570ca04ce37f2a19d84240d3a89");
}

void HashUnitTest::sha1test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha1", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("sha1"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports SHA1:" << providersToTest;
}

void HashUnitTest::sha1longtest()
{
    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha1", provider)) {
            // QTime t;
            // t.start();
            QByteArray fillerString;
            fillerString.fill('a', 1000);

            // This test extracted from OpenOffice.org 1.1.2, in sal/workben/t_digest.c
            // It basically reflects FIPS 180-2, Appendix A.3
            // Also as per AS 2805.13.3-2000 Appendix A
            QCA::Hash shaHash(QStringLiteral("sha1"), provider);
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("34aa973cd4c4daa4f61eeb2bdbad27316534016f"));

            QFile f1(QStringLiteral(TEST_DATA_DIR "/data/empty"));
            QVERIFY(f1.open(QIODevice::ReadOnly));
            {
                QCA::Hash hashObj(QStringLiteral("sha1"), provider);
                hashObj.update(&f1);
                QCOMPARE(QString(QCA::arrayToHex(hashObj.final().toByteArray())),
                         QStringLiteral("da39a3ee5e6b4b0d3255bfef95601890afd80709"));
            }

            QFile f2(QStringLiteral(TEST_DATA_DIR "/data/twobytes"));
            QVERIFY(f2.open(QIODevice::ReadOnly));
            {
                QCA::Hash hashObj(QStringLiteral("sha1"), provider);
                hashObj.update(&f2);
                QCOMPARE(QString(QCA::arrayToHex(hashObj.final().toByteArray())),
                         QStringLiteral("efbd6de3c51ca16094391e837bf52f7452593e5c"));
            }

            QFile f3(QStringLiteral(TEST_DATA_DIR "/data/twohundredbytes"));
            QVERIFY(f3.open(QIODevice::ReadOnly));
            {
                QCA::Hash hashObj(QStringLiteral("sha1"), provider);
                hashObj.update(&f3);
                QCOMPARE(QString(QCA::arrayToHex(hashObj.final().toByteArray())),
                         QStringLiteral("d636519dfb18d913acbe69fc3ee5a4c7ac870297"));
            }
        }
    }
}

void HashUnitTest::sha224test_data()
{
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    // These are as specified in FIPS 180-2, change notice 1

    // FIPS 180-2, Appendix B.1
    QTest::newRow("sha224(abc)") << QByteArray("abc")
                                 << QStringLiteral("23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7");

    // FIPS 180-2, Appendix B.2
    QTest::newRow("sha224(aq)") << QByteArray("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
                                << QStringLiteral("75388b16512776cc5dba5da1fd890150b0c6455cb4f58b1952522525");
}

void HashUnitTest::sha224test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha224", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("sha224"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports SHA224:" << providersToTest;
}

void HashUnitTest::sha224longtest()
{
    QByteArray fillerString;
    fillerString.fill('a', 1000);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha224", provider)) {
            QCA::Hash shaHash(QStringLiteral("sha224"), provider);

            // This basically reflects FIPS 180-2, change notice 1, section 3
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"));

            shaHash.clear();
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("20794655980c91d8bbb4c1ea97618a4bf03f42581948b2ee4ee7ad67"));
        }
    }
}

void HashUnitTest::sha256test_data()
{
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    // These are as specified in FIPS 180-2

    // FIPS 180-2, Appendix B.1
    QTest::newRow("sha256(abc)") << QByteArray("abc")
                                 << QStringLiteral("ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");

    // FIPS 180-2, Appendix B.2
    QTest::newRow("sha256(abc)") << QByteArray("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
                                 << QStringLiteral("248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
}

void HashUnitTest::sha256test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha256", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("sha256"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports SHA256:" << providersToTest;
}

void HashUnitTest::sha256longtest()
{
    QByteArray fillerString;
    fillerString.fill('a', 1000);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha256", provider)) {
            QCA::Hash shaHash(QStringLiteral("sha256"), provider);

            // This basically reflects FIPS 180-2, change notice 1, section 3
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"));

            shaHash.clear();
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0"));
        }
    }
}

void HashUnitTest::sha384test_data()
{
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    // These are as specified in FIPS 180-2, and from Aaron Gifford's SHA2 tests

    // FIPS 180-2, Appendix B.1
    QTest::newRow("sha384(abc)")
        << QByteArray("abc")
        << QStringLiteral(
               "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7");

    // FIPS 180-2, Appendix B.2
    QTest::newRow("sha384(a-u)")
        << QByteArray(
               "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrs"
               "tnopqrstu")
        << QStringLiteral(
               "09330c33f71147e83d192fc782cd1b4753111b173b3b05d22fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039");

    // Aaron Gifford, vector002.info
    QTest::newRow("sha384(a-q)")
        << QByteArray("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
        << QStringLiteral(
               "3391fdddfc8dc7393707a65b1b4709397cf8b1d162af05abfe8f450de5f36bc6b0455a8520bc4e6f5fe95b1fe3c8452b");
}

void HashUnitTest::sha384test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha384", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("sha384"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports SHA384:" << providersToTest;
}

void HashUnitTest::sha384longtest()
{
    QByteArray fillerString;
    fillerString.fill('a', 1000);

    foreach (QString provider, providersToTest) {
        if (!QCA::isSupported("sha384", provider)) {
            // QTime t;
            // t.start();
            QCA::Hash shaHash(QStringLiteral("sha384"), provider);

            // This basically reflects FIPS 180-2, change notice 1, section 3
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae"
                                    "97ddd87f3d8985"));

            shaHash.clear();
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("9d0e1809716474cb086e834e310a4a1ced149e9c00f248527972cec5704c2a5b07b8b3dc38ecc4ebae"
                                    "97ddd87f3d8985"));
            // qDebug() << "SHA384: " << provider << " elapsed " << t.elapsed();
        }
    }
}

// These are as specified in FIPS 180-2, and from Aaron Gifford's SHA2 tests
void HashUnitTest::sha512test_data()
{
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    // FIPS 180-2, Appendix C.1
    QTest::newRow("sha512(abc)") << QByteArray("abc")
                                 << QStringLiteral(
                                        "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1"
                                        "a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
    // FIPS 180-2, Appendix C.2
    QTest::newRow("sha512(a-u)") << QByteArray(
                                        "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmno"
                                        "pqklmnopqrlmnopqrsmnopqrstnopqrstu")
                                 << QStringLiteral(
                                        "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018501d289e4900f7"
                                        "e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909");

    // Aaron Gifford, vector002.info
    QTest::newRow("sha512(a-q)") << QByteArray("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
                                 << QStringLiteral(
                                        "204a8fc6dda82f0a0ced7beb8e08a41657c16ef468b228a8279be331a703c33596fd15c13b1b07"
                                        "f9aa1d3bea57789ca031ad85c7a71dd70354ec631238ca3445");
}

void HashUnitTest::sha512test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha512", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("sha512"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports SHA512:" << providersToTest;
}

void HashUnitTest::sha512longtest()
{
    QByteArray fillerString;
    fillerString.fill('a', 1000);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("sha512", provider)) {
            QCA::Hash shaHash(QStringLiteral("sha512"), provider);

            // This basically reflects FIPS 180-2, change notice 1, section 3
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4c"
                                    "b0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"));

            shaHash.clear();
            for (int i = 0; i < 1000; i++)
                shaHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(shaHash.final().toByteArray())),
                     QStringLiteral("e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973ebde0ff244877ea60a4c"
                                    "b0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b"));
        }
    }
}

// These are as specified in http://www.esat.kuleuven.ac.be/~bosselae/ripemd160.html
// ISO/IEC 10118-3 costs a bit of money.
void HashUnitTest::rmd160test_data()
{
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    QTest::newRow("rmd160()") << QByteArray("") << QStringLiteral("9c1185a5c5e9fc54612808977ee8f548b2258d31");
    QTest::newRow("rmd160(a)") << QByteArray("a") << QStringLiteral("0bdc9d2d256b3ee9daae347be6f4dc835a467ffe");
    QTest::newRow("rmd160(abc)") << QByteArray("abc") << QStringLiteral("8eb208f7e05d987a9b044a8e98c6b087f15a0bfc");
    QTest::newRow("rmd160(md)") << QByteArray("message digest")
                                << QStringLiteral("5d0689ef49d2fae572b881b123a85ffa21595f36");
    QTest::newRow("rmd160(a-z)") << QByteArray("abcdefghijklmnopqrstuvwxyz")
                                 << QStringLiteral("f71c27109c692c1b56bbdceb5b9d2865b3708dbc");
    QTest::newRow("rmd160(a-q)") << QByteArray("abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq")
                                 << QStringLiteral("12a053384a9c0c88e405a06c27dcf49ada62eb2b");
    QTest::newRow("rmd160(A-9)") << QByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
                                 << QStringLiteral("b0e20b6e3116640286ed3a87a5713079b21f5189");
    QTest::newRow("rmd160(1-0)")
        << QByteArray("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
        << QStringLiteral("9b752e45573d4b39f4dbd3323cab82bf63326bfb");
}

void HashUnitTest::rmd160test()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("ripemd160", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("ripemd160"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports RIPEMD160:" << providersToTest;
}

void HashUnitTest::rmd160longtest()
{
    QByteArray fillerString;
    fillerString.fill('a', 1000);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("ripemd160", provider)) {
            QCA::Hash rmdHash(QStringLiteral("ripemd160"), provider);

            // This is the "million times 'a' test"
            for (int i = 0; i < 1000; i++)
                rmdHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(rmdHash.final().toByteArray())),
                     QStringLiteral("52783243c1697bdbe16d37f97f68f08325dc1528"));

            rmdHash.clear();
            for (int i = 0; i < 1000; i++)
                rmdHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(rmdHash.final().toByteArray())),
                     QStringLiteral("52783243c1697bdbe16d37f97f68f08325dc1528"));

            // This is the "8 rounds of 1234567890" test.
            // It also ensure that we can re-use hash objects correctly.
            static char bindata[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30};
            QByteArray  fillerArray(bindata, sizeof(bindata)); // "1234567890"
            rmdHash.clear();
            for (int i = 0; i < 8; i++)
                rmdHash.update(fillerArray);
            QCOMPARE(QString(QCA::arrayToHex(rmdHash.final().toByteArray())),
                     QStringLiteral("9b752e45573d4b39f4dbd3323cab82bf63326bfb"));
        }
    }
}

// These are from the documentation pack at http://paginas.terra.com.br/informatica/paulobarreto/WhirlpoolPage.html
void HashUnitTest::whirlpooltest_data()
{
    QTest::addColumn<QByteArray>("input");
    QTest::addColumn<QString>("expectedHash");

    QTest::newRow("whirlpool()") << QByteArray("")
                                 << QStringLiteral(
                                        "19fa61d75522a4669b44e39c1d2e1726c530232130d407f89afee0964997f7a73e83be698b288f"
                                        "ebcf88e3e03c4f0757ea8964e59b63d93708b138cc42a66eb3");
    QTest::newRow("whirlpool(a)") << QByteArray("a")
                                  << QStringLiteral(
                                         "8aca2602792aec6f11a67206531fb7d7f0dff59413145e6973c45001d0087b42d11bc645413ae"
                                         "ff63a42391a39145a591a92200d560195e53b478584fdae231a");
    QTest::newRow("whirlpool(abc)") << QByteArray("abc")
                                    << QStringLiteral(
                                           "4e2448a4c6f486bb16b6562c73b4020bf3043e3a731bce721ae1b303d97e6d4c7181eebdb6c"
                                           "57e277d0e34957114cbd6c797fc9d95d8b582d225292076d4eef5");
    QTest::newRow("whirlpool(md)") << QByteArray("message digest")
                                   << QStringLiteral(
                                          "378c84a4126e2dc6e56dcc7458377aac838d00032230f53ce1f5700c0ffb4d3b8421557659ef"
                                          "55c106b4b52ac5a4aaa692ed920052838f3362e86dbd37a8903e");
    QTest::newRow("whirlpool(a-k)") << QByteArray("abcdbcdecdefdefgefghfghighijhijk")
                                    << QStringLiteral(
                                           "2a987ea40f917061f5d6f0a0e4644f488a7a5a52deee656207c562f988e95c6916bdc8031bc"
                                           "5be1b7b947639fe050b56939baaa0adff9ae6745b7b181c3be3fd");
    QTest::newRow("whirlpool(a-z)") << QByteArray("abcdefghijklmnopqrstuvwxyz")
                                    << QStringLiteral(
                                           "f1d754662636ffe92c82ebb9212a484a8d38631ead4238f5442ee13b8054e41b08bf2a9251c"
                                           "30b6a0b8aae86177ab4a6f68f673e7207865d5d9819a3dba4eb3b");
    QTest::newRow("whirlpool(A-9)") << QByteArray("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
                                    << QStringLiteral(
                                           "dc37e008cf9ee69bf11f00ed9aba26901dd7c28cdec066cc6af42e40f82f3a1e08eba266291"
                                           "29d8fb7cb57211b9281a65517cc879d7b962142c65f5a7af01467");
    QTest::newRow("whirlpool(1-0)")
        << QByteArray("12345678901234567890123456789012345678901234567890123456789012345678901234567890")
        << QStringLiteral(
               "466ef18babb0154d25b9d38a6414f5c08784372bccb204d6549c4afadb6014294d5bd8df2a6c44e538cd047b2681a51a2c60481"
               "e88c5a20b2c2a80cf3a9a083b");
}

void HashUnitTest::whirlpooltest()
{
    bool anyProviderTested = false;
    QFETCH(QByteArray, input);
    QFETCH(QString, expectedHash);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("whirlpool", provider)) {
            anyProviderTested = true;

            QCA::Hash hash = QCA::Hash(QStringLiteral("whirlpool"), provider);
            QCA::Hash copy = hash;
            hash.context(); // detach

            QCOMPARE(hash.hashToString(input), expectedHash);
            QCOMPARE(copy.hashToString(input), expectedHash);
        }
    }
    if (!anyProviderTested)
        qWarning() << "NONE of the providers supports Whirlpool:" << providersToTest;
}

void HashUnitTest::whirlpoollongtest()
{
    QByteArray fillerString;
    fillerString.fill('a', 1000);

    foreach (QString provider, providersToTest) {
        if (QCA::isSupported("whirlpool", provider)) {
            QCA::Hash rmdHash(QStringLiteral("whirlpool"), provider);

            // This is the "million times 'a' test"
            for (int i = 0; i < 1000; i++)
                rmdHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(rmdHash.final().toByteArray())),
                     QStringLiteral("0c99005beb57eff50a7cf005560ddf5d29057fd86b20bfd62deca0f1ccea4af51fc15490eddc47af32"
                                    "bb2b66c34ff9ad8c6008ad677f77126953b226e4ed8b01"));

            rmdHash.clear();
            for (int i = 0; i < 1000; i++)
                rmdHash.update(fillerString);
            QCOMPARE(QString(QCA::arrayToHex(rmdHash.final().toByteArray())),
                     QStringLiteral("0c99005beb57eff50a7cf005560ddf5d29057fd86b20bfd62deca0f1ccea4af51fc15490eddc47af32"
                                    "bb2b66c34ff9ad8c6008ad677f77126953b226e4ed8b01"));

            // This is the "8 rounds of 1234567890" test.
            // It also ensure that we can re-use hash objects correctly.
            static char bindata[] = {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30};
            QByteArray  fillerArray(bindata, sizeof(bindata)); // "1234567890"
            rmdHash.clear();
            for (int i = 0; i < 8; i++)
                rmdHash.update(fillerArray);
            QCOMPARE(QString(QCA::arrayToHex(rmdHash.final().toByteArray())),
                     QStringLiteral("466ef18babb0154d25b9d38a6414f5c08784372bccb204d6549c4afadb6014294d5bd8df2a6c44e538"
                                    "cd047b2681a51a2c60481e88c5a20b2c2a80cf3a9a083b"));
        }
    }
}

QTEST_MAIN(HashUnitTest)

#include "hashunittest.moc"