]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/sha256.h
* doc/apt-cache.8.xml:
[apt.git] / apt-pkg / contrib / sha256.h
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: sha1.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
4 /* ######################################################################
5
6 SHA256SumValue - Storage for a SHA-256 hash.
7 SHA256Summation - SHA-256 Secure Hash Algorithm.
8
9 This is a C++ interface to a set of SHA256Sum functions, that mirrors
10 the equivalent MD5 & SHA1 classes.
11
12 ##################################################################### */
13 /*}}}*/
14 #ifndef APTPKG_SHA256_H
15 #define APTPKG_SHA256_H
16
17 #include <string>
18 #include <cstring>
19 #include <algorithm>
20
21 using std::string;
22 using std::min;
23
24 class SHA256Summation;
25
26 class SHA256SumValue
27 {
28 friend class SHA256Summation;
29 unsigned char Sum[32];
30
31 public:
32
33 // Accessors
34 bool operator ==(const SHA256SumValue &rhs) const;
35 string Value() const;
36 inline void Value(unsigned char S[32])
37 {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
38 inline operator string() const {return Value();};
39 bool Set(string Str);
40 inline void Set(unsigned char S[32])
41 {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
42
43 SHA256SumValue(string Str);
44 SHA256SumValue();
45 };
46
47 struct sha256_ctx {
48 uint32_t count[2];
49 uint32_t state[8];
50 uint8_t buf[128];
51 };
52
53 class SHA256Summation
54 {
55 struct sha256_ctx Sum;
56
57 bool Done;
58
59 public:
60
61 bool Add(const unsigned char *inbuf,unsigned long inlen);
62 inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
63 bool AddFD(int Fd,unsigned long Size);
64 inline bool Add(const unsigned char *Beg,const unsigned char *End)
65 {return Add(Beg,End-Beg);};
66 SHA256SumValue Result();
67
68 SHA256Summation();
69 };
70
71 #endif