aboutsummaryrefslogtreecommitdiffstats
path: root/libtests/sha2.cc
blob: 65b70c39c25b6bc7a2cf979b83d73243149647d6 (plain) (blame)
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
#include <qpdf/Pl_SHA2.hh>
#include <qpdf/QUtil.hh>
#include <iostream>
#include <stdlib.h>
#include <string.h>

static void
test(
    Pl_SHA2& sha2,
    char const* description,
    int bits,
    char const* input,
    std::string const& output)
{
    sha2.resetBits(bits);
    sha2.write(QUtil::unsigned_char_pointer(input), strlen(input));
    sha2.finish();
    std::cout << description << ": ";
    if (output == sha2.getHexDigest()) {
        std::cout << "passed\n";
    } else {
        std::cout << "failed\n"
                  << "  expected: " << output << "\n"
                  << "  actual:   " << sha2.getHexDigest() << "\n";
    }
}

int
main(int argc, char* argv[])
{
    Pl_SHA2 sha2;
    char million_a[1000001];
    memset(million_a, 'a', 1000000);
    million_a[1000000] = '\0';
    test(
        sha2,
        "256 short",
        256,
        "abc",
        "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
    // cSpell:ignore abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq
    test(
        sha2,
        "256 long",
        256,
        "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
        "248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
    test(
        sha2,
        "256 million",
        256,
        million_a,
        "cdc76e5c9914fb9281a1c7e284d73e67f1809a48a497200e046d39ccc7112cd0");
    test(
        sha2,
        "384 short",
        384,
        "abc",
        "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded163"
        "1a8b605a43ff5bed8086072ba1e7cc2358baeca134c825a7");
    test(
        sha2,
        "384 long",
        384,
        "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
        "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
        "09330c33f71147e83d192fc782cd1b4753111b173b3b05d2"
        "2fa08086e3b0f712fcc7c71a557e2db966c3e9fa91746039");
    test(
        sha2,
        "384 million",
        384,
        million_a,
        "9d0e1809716474cb086e834e310a4a1ced149e9c00f24852"
        "7972cec5704c2a5b07b8b3dc38ecc4ebae97ddd87f3d8985");
    test(
        sha2,
        "512 short",
        512,
        "abc",
        "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a"
        "2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f");
    test(
        sha2,
        "512 long",
        512,
        "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
        "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu",
        "8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa17299aeadb6889018"
        "501d289e4900f7e4331b99dec4b5433ac7d329eeb6dd26545e96e55b874be909");
    test(
        sha2,
        "512 million",
        512,
        million_a,
        "e718483d0ce769644e2e42c7bc15b4638e1f98b13b2044285632a803afa973eb"
        "de0ff244877ea60a4cb0432ce577c31beb009c5c2c49aa2e4eadb217ad8cc09b");

    return 0;
}