]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/sha2.h
move sha512,256 into apt-pkg/sha2.{cc,h}, move gifford implementation to sha2_interna...
[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
24 using std::string;
25 using std::min;
26
27 // SHA512
28 class SHA512Summation;
29
30 class SHA512SumValue
31 {
32 friend class SHA512Summation;
33 unsigned char Sum[64];
34
35 public:
36
37 // Accessors
38 bool operator ==(const SHA512SumValue &rhs) const;
39 string Value() const;
40 inline void Value(unsigned char S[64])
41 {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
42 inline operator string() const {return Value();};
43 bool Set(string Str);
44 inline void Set(unsigned char S[64])
45 {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
46
47 SHA512SumValue(string Str);
48 SHA512SumValue();
49 };
50
51 class SHA512Summation
52 {
53 SHA512_CTX ctx;
54 unsigned char Sum[64];
55 bool Done;
56
57 public:
58
59 bool Add(const unsigned char *inbuf,unsigned long inlen);
60 inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
61 bool AddFD(int Fd,unsigned long Size);
62 inline bool Add(const unsigned char *Beg,const unsigned char *End)
63 {return Add(Beg,End-Beg);};
64 SHA512SumValue Result();
65
66 SHA512Summation();
67 };
68
69 // SHA256
70 class SHA256Summation;
71
72 class SHA256SumValue
73 {
74 friend class SHA256Summation;
75 unsigned char Sum[32];
76
77 public:
78
79 // Accessors
80 bool operator ==(const SHA256SumValue &rhs) const;
81 string Value() const;
82 inline void Value(unsigned char S[32])
83 {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
84 inline operator string() const {return Value();};
85 bool Set(string Str);
86 inline void Set(unsigned char S[32])
87 {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
88
89 SHA256SumValue(string Str);
90 SHA256SumValue();
91 };
92
93 class SHA256Summation
94 {
95 SHA256_CTX ctx;
96 unsigned char Sum[32];
97 bool Done;
98
99 public:
100
101 bool Add(const unsigned char *inbuf,unsigned long inlen);
102 inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
103 bool AddFD(int Fd,unsigned long Size);
104 inline bool Add(const unsigned char *Beg,const unsigned char *End)
105 {return Add(Beg,End-Beg);};
106 SHA256SumValue Result();
107
108 SHA256Summation();
109 };
110
111 #endif