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