Support large files in the complete toolset. Indexes of this
[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 };
68 };
69
70 class SHA512Summation : public SHA2SummationBase
71 {
72 SHA512_CTX ctx;
73 unsigned char Sum[64];
74
75 public:
76 bool Add(const unsigned char *inbuf, unsigned long long len)
77 {
78 if (Done)
79 return false;
80 SHA512_Update(&ctx, inbuf, len);
81 return true;
82 };
83 using SummationImplementation::Add;
84
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 };
100 };
101
102
103 #endif