summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/hashes.cc
diff options
context:
space:
mode:
authorJulian Andres Klode <julian.klode@canonical.com>2020-01-07 20:36:53 +0100
committerJulian Andres Klode <julian.klode@canonical.com>2020-01-14 13:10:36 +0100
commit79de3008ebfc6b4a5dd32e9de1d19788da0b885d (patch)
tree277129f384746a11c25a08d1b0b879c41fe207ad /apt-pkg/contrib/hashes.cc
parentb350560e34a369ef7610f9fceeffb00660209390 (diff)
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.
Diffstat (limited to 'apt-pkg/contrib/hashes.cc')
-rw-r--r--apt-pkg/contrib/hashes.cc25
1 files changed, 17 insertions, 8 deletions
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)) {}