Coverage Report

Created: 2026-09-14 06:17

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qpdf/libqpdf/SHA2_native.cc
Line
Count
Source
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
6.82M
    bits(bits)
9
6.82M
{
10
6.82M
    switch (bits) {
11
2.35M
    case 256:
12
2.35M
        sph_sha256_init(&ctx256);
13
2.35M
        break;
14
2.22M
    case 384:
15
2.22M
        sph_sha384_init(&ctx384);
16
2.22M
        break;
17
2.24M
    case 512:
18
2.24M
        sph_sha512_init(&ctx512);
19
2.24M
        break;
20
0
    default:
21
0
        badBits();
22
0
        break;
23
6.82M
    }
24
6.82M
}
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
6.94M
{
35
6.94M
    switch (bits) {
36
2.47M
    case 256:
37
2.47M
        sph_sha256(&ctx256, buf, len);
38
2.47M
        break;
39
2.22M
    case 384:
40
2.22M
        sph_sha384(&ctx384, buf, len);
41
2.22M
        break;
42
2.24M
    case 512:
43
2.24M
        sph_sha512(&ctx512, buf, len);
44
2.24M
        break;
45
0
    default:
46
0
        badBits();
47
0
        break;
48
6.94M
    }
49
6.94M
}
50
51
void
52
SHA2_native::finalize()
53
6.82M
{
54
6.82M
    switch (bits) {
55
2.35M
    case 256:
56
2.35M
        sph_sha256_close(&ctx256, sha256sum);
57
2.35M
        break;
58
2.22M
    case 384:
59
2.22M
        sph_sha384_close(&ctx384, sha384sum);
60
2.22M
        break;
61
2.24M
    case 512:
62
2.24M
        sph_sha512_close(&ctx512, sha512sum);
63
2.24M
        break;
64
0
    default:
65
0
        badBits();
66
0
        break;
67
6.82M
    }
68
6.82M
}
69
70
std::string
71
SHA2_native::getRawDigest()
72
6.82M
{
73
6.82M
    std::string result;
74
6.82M
    switch (bits) {
75
2.35M
    case 256:
76
2.35M
        result = std::string(reinterpret_cast<char*>(sha256sum), sizeof(sha256sum));
77
2.35M
        break;
78
2.22M
    case 384:
79
2.22M
        result = std::string(reinterpret_cast<char*>(sha384sum), sizeof(sha384sum));
80
2.22M
        break;
81
2.24M
    case 512:
82
2.24M
        result = std::string(reinterpret_cast<char*>(sha512sum), sizeof(sha512sum));
83
2.24M
        break;
84
0
    default:
85
0
        badBits();
86
0
        break;
87
6.82M
    }
88
6.82M
    return result;
89
6.82M
}