merge with debian-experimental
[ntk/apt.git] / apt-pkg / contrib / sha2.h
CommitLineData
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
MV
24
25using std::string;
26using std::min;
27
84a0890e 28class SHA512Summation;
7ac56f8f 29class SHA256Summation;
84a0890e 30
7ac56f8f
MV
31typedef HashSumValue<512> SHA512SumValue;
32typedef HashSumValue<256> SHA256SumValue;
84a0890e 33
31693a8f
MV
34class SHA2SummationBase
35{
36 protected:
37 bool Done;
38 public:
39 virtual bool Add(const unsigned char *inbuf,unsigned long inlen) = 0;
40 virtual bool AddFD(int Fd,unsigned long Size);
41
42 inline bool Add(const char *Data)
43 {
44 return Add((unsigned char *)Data,strlen(Data));
45 };
46 inline bool Add(const unsigned char *Beg,const unsigned char *End)
47 {
48 return Add(Beg,End-Beg);
49 };
50 void Result();
51};
52
53class SHA256Summation : public SHA2SummationBase
84a0890e 54{
7ac56f8f
MV
55 SHA256_CTX ctx;
56 unsigned char Sum[32];
84a0890e
MV
57
58 public:
31693a8f
MV
59 virtual bool Add(const unsigned char *inbuf, unsigned long len)
60 {
61 if (Done)
62 return false;
63 SHA256_Update(&ctx, inbuf, len);
64 return true;
65 };
66 SHA256SumValue Result()
67 {
68 if (!Done) {
69 SHA256_Final(Sum, &ctx);
70 Done = true;
71 }
72 SHA256SumValue res;
73 res.Set(Sum);
74 return res;
75 };
76 SHA256Summation()
77 {
78 SHA256_Init(&ctx);
79 Done = false;
80 };
84a0890e
MV
81};
82
31693a8f 83class SHA512Summation : public SHA2SummationBase
84a0890e 84{
7ac56f8f
MV
85 SHA512_CTX ctx;
86 unsigned char Sum[64];
84a0890e
MV
87
88 public:
31693a8f
MV
89 virtual bool Add(const unsigned char *inbuf, unsigned long len)
90 {
91 if (Done)
92 return false;
93 SHA512_Update(&ctx, inbuf, len);
94 return true;
95 };
96 SHA512SumValue Result()
97 {
98 if (!Done) {
99 SHA512_Final(Sum, &ctx);
100 Done = true;
101 }
102 SHA512SumValue res;
103 res.Set(Sum);
104 return res;
105 };
106 SHA512Summation()
107 {
108 SHA512_Init(&ctx);
109 Done = false;
110 };
84a0890e 111};
54ce88fd 112
7ac56f8f 113
54ce88fd 114#endif