| 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 | #include <stdint.h> |
| 21 | |
| 22 | using std::string; |
| 23 | using std::min; |
| 24 | |
| 25 | class SHA256Summation; |
| 26 | |
| 27 | class SHA256SumValue |
| 28 | { |
| 29 | friend class SHA256Summation; |
| 30 | unsigned char Sum[32]; |
| 31 | |
| 32 | public: |
| 33 | |
| 34 | // Accessors |
| 35 | bool operator ==(const SHA256SumValue &rhs) const; |
| 36 | string Value() const; |
| 37 | inline void Value(unsigned char S[32]) |
| 38 | {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];}; |
| 39 | inline operator string() const {return Value();}; |
| 40 | bool Set(string Str); |
| 41 | inline void Set(unsigned char S[32]) |
| 42 | {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];}; |
| 43 | |
| 44 | SHA256SumValue(string Str); |
| 45 | SHA256SumValue(); |
| 46 | }; |
| 47 | |
| 48 | struct sha256_ctx { |
| 49 | uint32_t count[2]; |
| 50 | uint32_t state[8]; |
| 51 | uint8_t buf[128]; |
| 52 | }; |
| 53 | |
| 54 | class SHA256Summation |
| 55 | { |
| 56 | struct sha256_ctx Sum; |
| 57 | |
| 58 | bool Done; |
| 59 | |
| 60 | public: |
| 61 | |
| 62 | bool Add(const unsigned char *inbuf,unsigned long inlen); |
| 63 | inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));}; |
| 64 | bool AddFD(int Fd,unsigned long Size); |
| 65 | inline bool Add(const unsigned char *Beg,const unsigned char *End) |
| 66 | {return Add(Beg,End-Beg);}; |
| 67 | SHA256SumValue Result(); |
| 68 | |
| 69 | SHA256Summation(); |
| 70 | }; |
| 71 | |
| 72 | #endif |