add hashsum support in apt-file download and add more tests
[ntk/apt.git] / apt-pkg / contrib / sha2.h
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"
23 #include "hashsum_template.h"
24
25 typedef HashSumValue<512> SHA512SumValue;
26 typedef HashSumValue<256> SHA256SumValue;
27
28 class SHA2SummationBase : public SummationImplementation
29 {
30 protected:
31 bool Done;
32 public:
33 bool Add(const unsigned char *inbuf, unsigned long long len) = 0;
34
35 void Result();
36 };
37
38 class SHA256Summation : public SHA2SummationBase
39 {
40 SHA256_CTX ctx;
41 unsigned char Sum[32];
42
43 public:
44 bool Add(const unsigned char *inbuf, unsigned long long len)
45 {
46 if (Done)
47 return false;
48 SHA256_Update(&ctx, inbuf, len);
49 return true;
50 };
51 using SummationImplementation::Add;
52
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 memset(&Sum, 0, sizeof(Sum));
68 };
69 };
70
71 class SHA512Summation : public SHA2SummationBase
72 {
73 SHA512_CTX ctx;
74 unsigned char Sum[64];
75
76 public:
77 bool Add(const unsigned char *inbuf, unsigned long long len)
78 {
79 if (Done)
80 return false;
81 SHA512_Update(&ctx, inbuf, len);
82 return true;
83 };
84 using SummationImplementation::Add;
85
86 SHA512SumValue Result()
87 {
88 if (!Done) {
89 SHA512_Final(Sum, &ctx);
90 Done = true;
91 }
92 SHA512SumValue res;
93 res.Set(Sum);
94 return res;
95 };
96 SHA512Summation()
97 {
98 SHA512_Init(&ctx);
99 Done = false;
100 memset(&Sum, 0, sizeof(Sum));
101 };
102 };
103
104
105 #endif