aboutsummaryrefslogtreecommitdiffstats
path: root/libqpdf/SHA2_native.cc
blob: 7386751e96cf1e041aebb35411ab60def8afb951 (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
#include <qpdf/SHA2_native.hh>

#include <qpdf/QUtil.hh>
#include <cstdio>
#include <stdexcept>

SHA2_native::SHA2_native(int bits) :
    bits(bits)
{
    switch (bits) {
    case 256:
        sph_sha256_init(&this->ctx256);
        break;
    case 384:
        sph_sha384_init(&this->ctx384);
        break;
    case 512:
        sph_sha512_init(&this->ctx512);
        break;
    default:
        badBits();
        break;
    }
}

void
SHA2_native::badBits()
{
    throw std::logic_error("SHA2_native has bits != 256, 384, or 512");
}

void
SHA2_native::update(unsigned char const* buf, size_t len)
{
    switch (bits) {
    case 256:
        sph_sha256(&this->ctx256, buf, len);
        break;
    case 384:
        sph_sha384(&this->ctx384, buf, len);
        break;
    case 512:
        sph_sha512(&this->ctx512, buf, len);
        break;
    default:
        badBits();
        break;
    }
}

void
SHA2_native::finalize()
{
    switch (bits) {
    case 256:
        sph_sha256_close(&this->ctx256, sha256sum);
        break;
    case 384:
        sph_sha384_close(&this->ctx384, sha384sum);
        break;
    case 512:
        sph_sha512_close(&this->ctx512, sha512sum);
        break;
    default:
        badBits();
        break;
    }
}

std::string
SHA2_native::getRawDigest()
{
    std::string result;
    switch (bits) {
    case 256:
        result = std::string(reinterpret_cast<char*>(this->sha256sum), sizeof(this->sha256sum));
        break;
    case 384:
        result = std::string(reinterpret_cast<char*>(this->sha384sum), sizeof(this->sha384sum));
        break;
    case 512:
        result = std::string(reinterpret_cast<char*>(this->sha512sum), sizeof(this->sha512sum));
        break;
    default:
        badBits();
        break;
    }
    return result;
}