mark as Automatic/Downloadable pure as gcc suggests
[ntk/apt.git] / apt-pkg / contrib / sha2.h
index 2c3fcae..a25ad4d 100644 (file)
 #ifndef APTPKG_SHA2_H
 #define APTPKG_SHA2_H
 
-#include <string>
 #include <cstring>
-#include <algorithm>
-#include <stdint.h>
 
 #include "sha2_internal.h"
 #include "hashsum_template.h"
 
-using std::string;
-using std::min;
+#ifndef APT_10_CLEANER_HEADERS
+#include <string>
+#include <algorithm>
+#include <stdint.h>
+#endif
 
-class SHA512Summation;
-class SHA256Summation;
 
 typedef HashSumValue<512> SHA512SumValue;
 typedef HashSumValue<256> SHA256SumValue;
 
-class SHA256Summation
+class SHA2SummationBase : public SummationImplementation
+{
+ protected:
+   bool Done;
+ public:
+   bool Add(const unsigned char *inbuf, unsigned long long len) = 0;
+
+   void Result();
+};
+
+class SHA256Summation : public SHA2SummationBase
 {
    SHA256_CTX ctx;
    unsigned char Sum[32];
-   bool Done;
 
    public:
+   bool Add(const unsigned char *inbuf, unsigned long long len)
+   {
+      if (Done) 
+         return false;
+      SHA256_Update(&ctx, inbuf, len);
+      return true;
+   };
+   using SummationImplementation::Add;
 
-   bool Add(const unsigned char *inbuf,unsigned long inlen);
-   inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
-   bool AddFD(int Fd,unsigned long Size);
-   inline bool Add(const unsigned char *Beg,const unsigned char *End) 
-                  {return Add(Beg,End-Beg);};
-   SHA256SumValue Result();
-   
-   SHA256Summation();
+   SHA256SumValue Result()
+   {
+      if (!Done) {
+         SHA256_Final(Sum, &ctx);
+         Done = true;
+      }
+      SHA256SumValue res;
+      res.Set(Sum);
+      return res;
+   };
+   SHA256Summation()
+   {
+      SHA256_Init(&ctx);
+      Done = false;
+      memset(&Sum, 0, sizeof(Sum));
+   };
 };
 
-class SHA512Summation
+class SHA512Summation : public SHA2SummationBase
 {
    SHA512_CTX ctx;
    unsigned char Sum[64];
-   bool Done;
 
    public:
+   bool Add(const unsigned char *inbuf, unsigned long long len)
+   {
+      if (Done) 
+         return false;
+      SHA512_Update(&ctx, inbuf, len);
+      return true;
+   };
+   using SummationImplementation::Add;
 
-   bool Add(const unsigned char *inbuf,unsigned long inlen);
-   inline bool Add(const char *Data) {return Add((unsigned char *)Data,strlen(Data));};
-   bool AddFD(int Fd,unsigned long Size);
-   inline bool Add(const unsigned char *Beg,const unsigned char *End) 
-                  {return Add(Beg,End-Beg);};
-   SHA512SumValue Result();
-   
-   SHA512Summation();
+   SHA512SumValue Result()
+   {
+      if (!Done) {
+         SHA512_Final(Sum, &ctx);
+         Done = true;
+      }
+      SHA512SumValue res;
+      res.Set(Sum);
+      return res;
+   };
+   SHA512Summation()
+   {
+      SHA512_Init(&ctx);
+      Done = false;
+      memset(&Sum, 0, sizeof(Sum));
+   };
 };