Coverage Report

Created: 2025-07-14 06:20

/src/qpdf/libqpdf/SHA2_native.cc
Line
Count
Source (jump to first uncovered line)
1
#include <qpdf/SHA2_native.hh>
2
3
#include <qpdf/QUtil.hh>
4
#include <cstdio>
5
#include <stdexcept>
6
7
SHA2_native::SHA2_native(int bits) :
8
5.74M
    bits(bits)
9
5.74M
{
10
5.74M
    switch (bits) {
11
1.97M
    case 256:
12
1.97M
        sph_sha256_init(&ctx256);
13
1.97M
        break;
14
1.89M
    case 384:
15
1.89M
        sph_sha384_init(&ctx384);
16
1.89M
        break;
17
1.87M
    case 512:
18
1.87M
        sph_sha512_init(&ctx512);
19
1.87M
        break;
20
0
    default:
21
0
        badBits();
22
0
        break;
23
5.74M
    }
24
5.74M
}
25
26
void
27
SHA2_native::badBits()
28
0
{
29
0
    throw std::logic_error("SHA2_native has bits != 256, 384, or 512");
30
0
}
31
32
void
33
SHA2_native::update(unsigned char const* buf, size_t len)
34
5.84M
{
35
5.84M
    switch (bits) {
36
2.07M
    case 256:
37
2.07M
        sph_sha256(&ctx256, buf, len);
38
2.07M
        break;
39
1.89M
    case 384:
40
1.89M
        sph_sha384(&ctx384, buf, len);
41
1.89M
        break;
42
1.87M
    case 512:
43
1.87M
        sph_sha512(&ctx512, buf, len);
44
1.87M
        break;
45
0
    default:
46
0
        badBits();
47
0
        break;
48
5.84M
    }
49
5.84M
}
50
51
void
52
SHA2_native::finalize()
53
5.74M
{
54
5.74M
    switch (bits) {
55
1.97M
    case 256:
56
1.97M
        sph_sha256_close(&ctx256, sha256sum);
57
1.97M
        break;
58
1.89M
    case 384:
59
1.89M
        sph_sha384_close(&ctx384, sha384sum);
60
1.89M
        break;
61
1.87M
    case 512:
62
1.87M
        sph_sha512_close(&ctx512, sha512sum);
63
1.87M
        break;
64
0
    default:
65
0
        badBits();
66
0
        break;
67
5.74M
    }
68
5.74M
}
69
70
std::string
71
SHA2_native::getRawDigest()
72
5.74M
{
73
5.74M
    std::string result;
74
5.74M
    switch (bits) {
75
1.97M
    case 256:
76
1.97M
        result = std::string(reinterpret_cast<char*>(sha256sum), sizeof(sha256sum));
77
1.97M
        break;
78
1.89M
    case 384:
79
1.89M
        result = std::string(reinterpret_cast<char*>(sha384sum), sizeof(sha384sum));
80
1.89M
        break;
81
1.87M
    case 512:
82
1.87M
        result = std::string(reinterpret_cast<char*>(sha512sum), sizeof(sha512sum));
83
1.87M
        break;
84
0
    default:
85
0
        badBits();
86
0
        break;
87
5.74M
    }
88
5.74M
    return result;
89
5.74M
}