]>
Commit | Line | Data |
---|---|---|
84a0890e MV |
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 <string> | |
18 | #include <cstring> | |
19 | #include <algorithm> | |
20 | #include <stdint.h> | |
21 | ||
22 | #include "sha2_internal.h" | |
7ac56f8f | 23 | #include "hashsum_template.h" |
84a0890e | 24 | |
7ac56f8f MV |
25 | typedef HashSumValue<512> SHA512SumValue; |
26 | typedef HashSumValue<256> SHA256SumValue; | |
84a0890e | 27 | |
c31c1dde | 28 | class SHA2SummationBase : public SummationImplementation |
31693a8f MV |
29 | { |
30 | protected: | |
31 | bool Done; | |
32 | public: | |
650faab0 | 33 | bool Add(const unsigned char *inbuf, unsigned long long len) = 0; |
31693a8f | 34 | |
31693a8f MV |
35 | void Result(); |
36 | }; | |
37 | ||
38 | class SHA256Summation : public SHA2SummationBase | |
84a0890e | 39 | { |
7ac56f8f MV |
40 | SHA256_CTX ctx; |
41 | unsigned char Sum[32]; | |
84a0890e MV |
42 | |
43 | public: | |
650faab0 | 44 | bool Add(const unsigned char *inbuf, unsigned long long len) |
31693a8f MV |
45 | { |
46 | if (Done) | |
47 | return false; | |
48 | SHA256_Update(&ctx, inbuf, len); | |
49 | return true; | |
50 | }; | |
c31c1dde DK |
51 | using SummationImplementation::Add; |
52 | ||
31693a8f MV |
53 | SHA256SumValue Result() |
54 | { | |
55 | if (!Done) { | |
56 | SHA256_Final(Sum, &ctx); | |
57 | Done = true; | |
58 | } | |
59 | SHA256SumValue res; | |
60 | res.Set(Sum); | |
61 | return res; | |
62 | }; | |
63 | SHA256Summation() | |
64 | { | |
65 | SHA256_Init(&ctx); | |
66 | Done = false; | |
67 | }; | |
84a0890e MV |
68 | }; |
69 | ||
31693a8f | 70 | class SHA512Summation : public SHA2SummationBase |
84a0890e | 71 | { |
7ac56f8f MV |
72 | SHA512_CTX ctx; |
73 | unsigned char Sum[64]; | |
84a0890e MV |
74 | |
75 | public: | |
650faab0 | 76 | bool Add(const unsigned char *inbuf, unsigned long long len) |
31693a8f MV |
77 | { |
78 | if (Done) | |
79 | return false; | |
80 | SHA512_Update(&ctx, inbuf, len); | |
81 | return true; | |
82 | }; | |
c31c1dde DK |
83 | using SummationImplementation::Add; |
84 | ||
31693a8f MV |
85 | SHA512SumValue Result() |
86 | { | |
87 | if (!Done) { | |
88 | SHA512_Final(Sum, &ctx); | |
89 | Done = true; | |
90 | } | |
91 | SHA512SumValue res; | |
92 | res.Set(Sum); | |
93 | return res; | |
94 | }; | |
95 | SHA512Summation() | |
96 | { | |
97 | SHA512_Init(&ctx); | |
98 | Done = false; | |
99 | }; | |
84a0890e | 100 | }; |
54ce88fd | 101 | |
7ac56f8f | 102 | |
54ce88fd | 103 | #endif |