X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/7ac56f8ffd5544c6c1f681f79cafbf72d37d0b82..259d88d94843fbce7c8100f8b337b7b8fb58b840:/apt-pkg/contrib/sha2.h diff --git a/apt-pkg/contrib/sha2.h b/apt-pkg/contrib/sha2.h index 2c3fcae12..8e0c99a1b 100644 --- a/apt-pkg/contrib/sha2.h +++ b/apt-pkg/contrib/sha2.h @@ -22,49 +22,83 @@ #include "sha2_internal.h" #include "hashsum_template.h" -using std::string; -using std::min; - -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)); + }; };