apt @VERSION@

sha256.h

00001 // -*- mode: cpp; mode: fold -*-
00002 // Description                                                          /*{{{*/
00003 // $Id: sha1.h,v 1.3 2001/05/07 05:05:47 jgg Exp $
00004 /* ######################################################################
00005 
00006    SHA256SumValue - Storage for a SHA-256 hash.
00007    SHA256Summation - SHA-256 Secure Hash Algorithm.
00008    
00009    This is a C++ interface to a set of SHA256Sum functions, that mirrors
00010    the equivalent MD5 & SHA1 classes. 
00011 
00012    ##################################################################### */
00013                                                                         /*}}}*/
00014 #ifndef APTPKG_SHA256_H
00015 #define APTPKG_SHA256_H
00016 
00017 #include <string>
00018 #include <cstring>
00019 #include <algorithm>
00020 #include <stdint.h>
00021 
00022 using std::string;
00023 using std::min;
00024 
00025 class SHA256Summation;
00026 
00027 class SHA256SumValue
00028 {
00029    friend class SHA256Summation;
00030    unsigned char Sum[32];
00031    
00032    public:
00033 
00034    // Accessors
00035    bool operator ==(const SHA256SumValue &rhs) const; 
00036    string Value() const;
00037    inline void Value(unsigned char S[32])
00038          {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];};
00039    inline operator string() const {return Value();};
00040    bool Set(string Str);
00041    inline void Set(unsigned char S[32]) 
00042          {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];};
00043 
00044    SHA256SumValue(string Str);
00045    SHA256SumValue();
00046 };
00047 
00048 struct sha256_ctx {
00049     uint32_t count[2];
00050     uint32_t state[8];
00051     uint8_t buf[128];
00052 };
00053 
00054 class SHA256Summation
00055 {
00056    struct sha256_ctx Sum;
00057 
00058    bool Done;
00059 
00060    public:
00061 
00062    bool Add(const unsigned char *inbuf,unsigned long inlen);
00063    inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
00064    bool AddFD(int Fd,unsigned long Size);
00065    inline bool Add(const unsigned char *Beg,const unsigned char *End) 
00066                   {return Add(Beg,End-Beg);};
00067    SHA256SumValue Result();
00068    
00069    SHA256Summation();
00070 };
00071 
00072 #endif