X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/84a0890e6ef49b5d41a0b9ff0b5a5fe95cca6f3e..3b5607fc31371190470074371793cb8500b5139e:/apt-pkg/contrib/sha2.h?ds=inline diff --git a/apt-pkg/contrib/sha2.h b/apt-pkg/contrib/sha2.h index 5148b05c3..a25ad4d32 100644 --- a/apt-pkg/contrib/sha2.h +++ b/apt-pkg/contrib/sha2.h @@ -14,98 +14,96 @@ #ifndef APTPKG_SHA2_H #define APTPKG_SHA2_H -#include #include -#include -#include #include "sha2_internal.h" +#include "hashsum_template.h" -using std::string; -using std::min; - -// SHA512 -class SHA512Summation; +#ifndef APT_10_CLEANER_HEADERS +#include +#include +#include +#endif -class SHA512SumValue -{ - friend class SHA512Summation; - unsigned char Sum[64]; - - public: - // Accessors - bool operator ==(const SHA512SumValue &rhs) const; - string Value() const; - inline void Value(unsigned char S[64]) - {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];}; - inline operator string() const {return Value();}; - bool Set(string Str); - inline void Set(unsigned char S[64]) - {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];}; - - SHA512SumValue(string Str); - SHA512SumValue(); -}; +typedef HashSumValue<512> SHA512SumValue; +typedef HashSumValue<256> SHA256SumValue; -class SHA512Summation +class SHA2SummationBase : public SummationImplementation { - SHA512_CTX ctx; - unsigned char Sum[64]; + protected: bool Done; + public: + bool Add(const unsigned char *inbuf, unsigned long long len) = 0; - public: - - 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(); + void Result(); }; -// SHA256 -class SHA256Summation; - -class SHA256SumValue +class SHA256Summation : public SHA2SummationBase { - friend class SHA256Summation; + SHA256_CTX ctx; unsigned char Sum[32]; - - public: - // Accessors - bool operator ==(const SHA256SumValue &rhs) const; - string Value() const; - inline void Value(unsigned char S[32]) - {for (int I = 0; I != sizeof(Sum); I++) S[I] = Sum[I];}; - inline operator string() const {return Value();}; - bool Set(string Str); - inline void Set(unsigned char S[32]) - {for (int I = 0; I != sizeof(Sum); I++) Sum[I] = S[I];}; - - SHA256SumValue(string Str); - SHA256SumValue(); + 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; + + 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 SHA256Summation +class SHA512Summation : public SHA2SummationBase { - SHA256_CTX ctx; - unsigned char Sum[32]; - bool Done; + SHA512_CTX ctx; + unsigned char Sum[64]; public: - - 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(); + bool Add(const unsigned char *inbuf, unsigned long long len) + { + if (Done) + return false; + SHA512_Update(&ctx, inbuf, len); + return true; + }; + using SummationImplementation::Add; + + 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)); + }; }; + #endif