From 79de3008ebfc6b4a5dd32e9de1d19788da0b885d Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 7 Jan 2020 20:36:53 +0100 Subject: Convert users of {MD5,SHA1,SHA256,SHA512}Summation to use Hashes This makes use of the a function GetHashString() that returns the specific hash string. We also need to implement another overload of Add() for signed chars with sizes, so the existing users do not require reinterpret_cast everywhere. --- apt-pkg/contrib/cdromutl.cc | 6 +++--- apt-pkg/contrib/hashes.cc | 25 +++++++++++++++++-------- apt-pkg/contrib/hashes.h | 7 +++++++ apt-pkg/deb/deblistparser.cc | 6 +++--- apt-private/private-show.cc | 5 +++-- ftparchive/multicompress.cc | 9 ++++----- ftparchive/writer.cc | 4 ++-- 7 files changed, 39 insertions(+), 23 deletions(-) diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 9db3980da..c0fe869d2 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include @@ -181,7 +181,7 @@ bool MountCdrom(string Path, string DeviceName) from effecting the outcome. */ bool IdentCdrom(string CD,string &Res,unsigned int Version) { - MD5Summation Hash; + Hashes Hash(Hashes::MD5SUM); bool writable_media = false; int dirfd = open(CD.c_str(), O_RDONLY | O_DIRECTORY | O_CLOEXEC); @@ -254,7 +254,7 @@ bool IdentCdrom(string CD,string &Res,unsigned int Version) strprintf(S, "-%u.debug", Version); closedir(D); - Res = Hash.Result().Value().append(std::move(S)); + Res = Hash.GetHashString(Hashes::MD5SUM).HashValue().append(std::move(S)); return true; } /*}}}*/ diff --git a/apt-pkg/contrib/hashes.cc b/apt-pkg/contrib/hashes.cc index 8ddaaf549..d506a1361 100644 --- a/apt-pkg/contrib/hashes.cc +++ b/apt-pkg/contrib/hashes.cc @@ -102,27 +102,27 @@ std::string HashString::GetHashForFile(std::string filename) const /*{{{*/ FileFd Fd(filename, FileFd::ReadOnly); if(strcasecmp(Type.c_str(), "MD5Sum") == 0) { - MD5Summation MD5; + Hashes MD5(Hashes::MD5SUM); MD5.AddFD(Fd); - fileHash = (std::string)MD5.Result(); + fileHash = MD5.GetHashString(Hashes::MD5SUM).Hash; } else if (strcasecmp(Type.c_str(), "SHA1") == 0) { - SHA1Summation SHA1; + Hashes SHA1(Hashes::SHA1SUM); SHA1.AddFD(Fd); - fileHash = (std::string)SHA1.Result(); + fileHash = SHA1.GetHashString(Hashes::SHA1SUM).Hash; } else if (strcasecmp(Type.c_str(), "SHA256") == 0) { - SHA256Summation SHA256; + Hashes SHA256(Hashes::SHA256SUM); SHA256.AddFD(Fd); - fileHash = (std::string)SHA256.Result(); + fileHash = SHA256.GetHashString(Hashes::SHA256SUM).Hash; } else if (strcasecmp(Type.c_str(), "SHA512") == 0) { - SHA512Summation SHA512; + Hashes SHA512(Hashes::SHA512SUM); SHA512.AddFD(Fd); - fileHash = (std::string)SHA512.Result(); + fileHash = SHA512.GetHashString(Hashes::SHA512SUM).Hash; } else if (strcasecmp(Type.c_str(), "Checksum-FileSize") == 0) strprintf(fileHash, "%llu", Fd.FileSize()); @@ -413,6 +413,15 @@ HashStringList Hashes::GetHashStringList() return hashes; } + +HashString Hashes::GetHashString(SupportedHashes hash) +{ + for (auto & Algo : Algorithms) + if (hash == Algo.ourAlgo) + return HashString(Algo.name, HexDigest(d->hd, Algo.gcryAlgo)); + + abort(); +} Hashes::Hashes() : d(new PrivateHashes(~0)) { } Hashes::Hashes(unsigned int const Hashes) : d(new PrivateHashes(Hashes)) {} Hashes::Hashes(HashStringList const &Hashes) : d(new PrivateHashes(Hashes)) {} diff --git a/apt-pkg/contrib/hashes.h b/apt-pkg/contrib/hashes.h index 14100e13b..07ccc6900 100644 --- a/apt-pkg/contrib/hashes.h +++ b/apt-pkg/contrib/hashes.h @@ -184,6 +184,10 @@ class Hashes bool Add(const unsigned char * const Data, unsigned long long const Size) APT_NONNULL(2); inline bool Add(const char * const Data) APT_NONNULL(2) {return Add(reinterpret_cast(Data),strlen(Data));}; + inline bool Add(const char *const Data, unsigned long long const Size) APT_NONNULL(2) + { + return Add(reinterpret_cast(Data), Size); + }; inline bool Add(const unsigned char * const Beg,const unsigned char * const End) APT_NONNULL(2,3) {return Add(Beg,End-Beg);}; @@ -194,6 +198,9 @@ class Hashes HashStringList GetHashStringList(); + /** Get a specific hash. It is an error to use a hash that was not hashes */ + HashString GetHashString(SupportedHashes hash); + /** create a Hashes object to calculate all supported hashes * * If ALL is too much, you can limit which Hashes are calculated diff --git a/apt-pkg/deb/deblistparser.cc b/apt-pkg/deb/deblistparser.cc index 88b41ad30..21d1736e4 100644 --- a/apt-pkg/deb/deblistparser.cc +++ b/apt-pkg/deb/deblistparser.cc @@ -17,8 +17,8 @@ #include #include #include +#include #include -#include #include #include #include @@ -298,10 +298,10 @@ APT::StringView debListParser::Description_md5() if (desc == "\n") return StringView(); - MD5Summation md5; + Hashes md5(Hashes::MD5SUM); md5.Add(desc.data(), desc.size()); md5.Add("\n"); - MD5Buffer = md5.Result(); + MD5Buffer = md5.GetHashString(Hashes::MD5SUM).HashValue(); return StringView(MD5Buffer); } else if (likely(value.size() == 32)) diff --git a/apt-private/private-show.cc b/apt-private/private-show.cc index 9ebbe6ac0..103fa57e4 100644 --- a/apt-private/private-show.cc +++ b/apt-private/private-show.cc @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -415,9 +416,9 @@ bool ShowPackage(CommandLine &CmdL) /*{{{*/ static std::string Sha1FromString(std::string const &input) /*{{{*/ { // XXX: move to hashes.h: HashString::FromString() ? - SHA1Summation sha1; + Hashes sha1(Hashes::SHA1SUM); sha1.Add(input.c_str(), input.length()); - return sha1.Result().Value(); + return sha1.GetHashString(Hashes::SHA1SUM).HashValue(); } /*}}}*/ bool ShowSrcPackage(CommandLine &CmdL) /*{{{*/ diff --git a/ftparchive/multicompress.cc b/ftparchive/multicompress.cc index f5fe14164..cdaa7a60a 100644 --- a/ftparchive/multicompress.cc +++ b/ftparchive/multicompress.cc @@ -18,8 +18,7 @@ #include #include #include -#include -#include +#include #include #include @@ -267,7 +266,7 @@ bool MultiCompress::Child(int const &FD) SetNonBlock(FD,false); unsigned char Buffer[32*1024]; unsigned long long FileSize = 0; - MD5Summation MD5; + Hashes MD5(Hashes::MD5SUM); while (1) { WaitFd(FD,false); @@ -315,7 +314,7 @@ bool MultiCompress::Child(int const &FD) } // Compute the hash - MD5Summation OldMD5; + Hashes OldMD5(Hashes::MD5SUM); unsigned long long NewFileSize = 0; while (1) { @@ -330,7 +329,7 @@ bool MultiCompress::Child(int const &FD) CompFd.Close(); // Check the hash - if (OldMD5.Result() == MD5.Result() && + if (OldMD5.GetHashString(Hashes::MD5SUM) == MD5.GetHashString(Hashes::MD5SUM) && FileSize == NewFileSize) { for (Files *I = Outputs; I != 0; I = I->Next) diff --git a/ftparchive/writer.cc b/ftparchive/writer.cc index 078638c41..edf548f3a 100644 --- a/ftparchive/writer.cc +++ b/ftparchive/writer.cc @@ -493,9 +493,9 @@ bool PackagesWriter::DoPackage(string FileName) string DescriptionMd5; if (LongDescription == false) { - MD5Summation descmd5; + Hashes descmd5(Hashes::MD5SUM); descmd5.Add(desc.c_str()); - DescriptionMd5 = descmd5.Result().Value(); + DescriptionMd5 = descmd5.GetHashString(Hashes::MD5SUM).HashValue(); Changes.push_back(pkgTagSection::Tag::Rewrite("Description-md5", DescriptionMd5)); if (TransWriter != NULL) TransWriter->DoPackage(Package, desc, DescriptionMd5); -- cgit v1.2.3