]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: sha512.h,v 1.3 2001/05/07 05:05:47 jgg Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | SHA{512,256}SumValue - Storage for a SHA-{512,256} hash. | |
7 | SHA{512,256}Summation - SHA-{512,256} Secure Hash Algorithm. | |
8 | ||
9 | This is a C++ interface to a set of SHA{512,256}Sum functions, that mirrors | |
10 | the equivalent MD5 & SHA1 classes. | |
11 | ||
12 | ##################################################################### */ | |
13 | /*}}}*/ | |
14 | #ifndef APTPKG_SHA2_H | |
15 | #define APTPKG_SHA2_H | |
16 | ||
17 | #include <cstring> | |
18 | ||
19 | #include "sha2_internal.h" | |
20 | #include "hashsum_template.h" | |
21 | ||
22 | #ifndef APT_10_CLEANER_HEADERS | |
23 | #include <string> | |
24 | #include <algorithm> | |
25 | #include <stdint.h> | |
26 | #endif | |
27 | ||
28 | ||
29 | typedef HashSumValue<512> SHA512SumValue; | |
30 | typedef HashSumValue<256> SHA256SumValue; | |
31 | ||
32 | class SHA2SummationBase : public SummationImplementation | |
33 | { | |
34 | protected: | |
35 | bool Done; | |
36 | public: | |
37 | bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE = 0; | |
38 | ||
39 | void Result(); | |
40 | }; | |
41 | ||
42 | class SHA256Summation : public SHA2SummationBase | |
43 | { | |
44 | SHA256_CTX ctx; | |
45 | unsigned char Sum[32]; | |
46 | ||
47 | public: | |
48 | bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE | |
49 | { | |
50 | if (Done) | |
51 | return false; | |
52 | SHA256_Update(&ctx, inbuf, len); | |
53 | return true; | |
54 | }; | |
55 | using SummationImplementation::Add; | |
56 | ||
57 | SHA256SumValue Result() | |
58 | { | |
59 | if (!Done) { | |
60 | SHA256_Final(Sum, &ctx); | |
61 | Done = true; | |
62 | } | |
63 | SHA256SumValue res; | |
64 | res.Set(Sum); | |
65 | return res; | |
66 | }; | |
67 | SHA256Summation() | |
68 | { | |
69 | SHA256_Init(&ctx); | |
70 | Done = false; | |
71 | memset(&Sum, 0, sizeof(Sum)); | |
72 | }; | |
73 | }; | |
74 | ||
75 | class SHA512Summation : public SHA2SummationBase | |
76 | { | |
77 | SHA512_CTX ctx; | |
78 | unsigned char Sum[64]; | |
79 | ||
80 | public: | |
81 | bool Add(const unsigned char *inbuf, unsigned long long len) APT_OVERRIDE | |
82 | { | |
83 | if (Done) | |
84 | return false; | |
85 | SHA512_Update(&ctx, inbuf, len); | |
86 | return true; | |
87 | }; | |
88 | using SummationImplementation::Add; | |
89 | ||
90 | SHA512SumValue Result() | |
91 | { | |
92 | if (!Done) { | |
93 | SHA512_Final(Sum, &ctx); | |
94 | Done = true; | |
95 | } | |
96 | SHA512SumValue res; | |
97 | res.Set(Sum); | |
98 | return res; | |
99 | }; | |
100 | SHA512Summation() | |
101 | { | |
102 | SHA512_Init(&ctx); | |
103 | Done = false; | |
104 | memset(&Sum, 0, sizeof(Sum)); | |
105 | }; | |
106 | }; | |
107 | ||
108 | ||
109 | #endif |