From 5420278dc884c0382f271872b67c33978f3fe6b8 Mon Sep 17 00:00:00 2001 From: Brad King Date: Thu, 3 Nov 2016 13:21:41 -0400 Subject: Port hash computation to cmCryptoHash Avoid using KWSys MD5 or `cm_sha2` and use the `cmCryptoHash` abstraction instead. --- Source/CPack/WiX/cmCPackWIXGenerator.cxx | 4 ++-- Source/CTest/cmCTestLaunch.cxx | 15 ++++++--------- Source/cmFileCommand.cxx | 3 ++- Source/cmFilePathUuid.cxx | 4 ++-- Source/cmGlobalGenerator.cxx | 13 ++++--------- Source/cmLocalGenerator.cxx | 16 +++------------- Source/cmSystemTools.cxx | 8 ++++---- Source/cmUuid.cxx | 31 +++++++++++-------------------- 8 files changed, 34 insertions(+), 60 deletions(-) diff --git a/Source/CPack/WiX/cmCPackWIXGenerator.cxx b/Source/CPack/WiX/cmCPackWIXGenerator.cxx index 4c8abd9..0c4f573 100644 --- a/Source/CPack/WiX/cmCPackWIXGenerator.cxx +++ b/Source/CPack/WiX/cmCPackWIXGenerator.cxx @@ -1060,8 +1060,8 @@ std::string cmCPackWIXGenerator::CreateNewIdForPath(std::string const& path) std::string cmCPackWIXGenerator::CreateHashedId( std::string const& path, std::string const& normalizedFilename) { - CM_AUTO_PTR sha1 = cmCryptoHash::New("SHA1"); - std::string hash = sha1->HashString(path.c_str()); + cmCryptoHash sha1(cmCryptoHash::AlgoSHA1); + std::string const hash = sha1.HashString(path); std::string identifier; identifier += hash.substr(0, 7) + "_"; diff --git a/Source/CTest/cmCTestLaunch.cxx b/Source/CTest/cmCTestLaunch.cxx index 224681a..f7a6e0b 100644 --- a/Source/CTest/cmCTestLaunch.cxx +++ b/Source/CTest/cmCTestLaunch.cxx @@ -4,6 +4,7 @@ #include +#include "cmCryptoHash.h" #include "cmGeneratedFileStream.h" #include "cmGlobalGenerator.h" #include "cmMakefile.h" @@ -14,7 +15,6 @@ #include #include -#include #include #include #include @@ -167,17 +167,14 @@ void cmCTestLaunch::ComputeFileNames() // We hash the input command working dir and command line to obtain // a repeatable and (probably) unique name for log files. - char hash[32]; - cmsysMD5* md5 = cmsysMD5_New(); - cmsysMD5_Initialize(md5); - cmsysMD5_Append(md5, (unsigned char const*)(this->CWD.c_str()), -1); + cmCryptoHash md5(cmCryptoHash::AlgoMD5); + md5.Initialize(); + md5.Append(this->CWD); for (std::vector::const_iterator ai = this->RealArgs.begin(); ai != this->RealArgs.end(); ++ai) { - cmsysMD5_Append(md5, (unsigned char const*)ai->c_str(), -1); + md5.Append(*ai); } - cmsysMD5_FinalizeHex(md5, hash); - cmsysMD5_Delete(md5); - this->LogHash.assign(hash, 32); + this->LogHash = md5.FinalizeHex(); // We store stdout and stderr in temporary log files. this->LogOut = this->LogDir; diff --git a/Source/cmFileCommand.cxx b/Source/cmFileCommand.cxx index 1bade57..615bd23 100644 --- a/Source/cmFileCommand.cxx +++ b/Source/cmFileCommand.cxx @@ -2553,7 +2553,8 @@ bool cmFileCommand::HandleDownloadCommand(std::vector const& args) this->SetError("DOWNLOAD missing sum value for EXPECTED_MD5."); return false; } - hash = CM_AUTO_PTR(cmCryptoHash::New("MD5")); + hash = + CM_AUTO_PTR(new cmCryptoHash(cmCryptoHash::AlgoMD5)); hashMatchMSG = "MD5 sum"; expectedHash = cmSystemTools::LowerCase(*i); } else if (*i == "SHOW_PROGRESS") { diff --git a/Source/cmFilePathUuid.cxx b/Source/cmFilePathUuid.cxx index ad434e3..03d2524 100644 --- a/Source/cmFilePathUuid.cxx +++ b/Source/cmFilePathUuid.cxx @@ -107,8 +107,8 @@ std::string cmFilePathUuid::GetChecksumString( { // Calculate the file ( seed + relative path + name ) checksum std::vector hashBytes = - cmCryptoHash::New("SHA256")->ByteHashString( - sourceRelSeed + sourceRelPath + sourceFilename); + cmCryptoHash(cmCryptoHash::AlgoSHA256) + .ByteHashString(sourceRelSeed + sourceRelPath + sourceFilename); checksumBase32 = cmBase32Encoder().encodeString(&hashBytes[0], hashBytes.size(), false); diff --git a/Source/cmGlobalGenerator.cxx b/Source/cmGlobalGenerator.cxx index cf51c6a..42e9df1 100644 --- a/Source/cmGlobalGenerator.cxx +++ b/Source/cmGlobalGenerator.cxx @@ -44,9 +44,9 @@ #include #if defined(CMAKE_BUILD_WITH_CMAKE) +#include "cmCryptoHash.h" #include #include -#include #endif class cmInstalledFile; @@ -2616,14 +2616,9 @@ void cmGlobalGenerator::AddRuleHash(const std::vector& outputs, // Compute a hash of the rule. RuleHash hash; { - unsigned char const* data = - reinterpret_cast(content.c_str()); - int length = static_cast(content.length()); - cmsysMD5* sum = cmsysMD5_New(); - cmsysMD5_Initialize(sum); - cmsysMD5_Append(sum, data, length); - cmsysMD5_FinalizeHex(sum, hash.Data); - cmsysMD5_Delete(sum); + cmCryptoHash md5(cmCryptoHash::AlgoMD5); + std::string const md5_hex = md5.HashString(content); + memcpy(hash.Data, md5_hex.c_str(), 32); } // Shorten the output name (in expected use case). diff --git a/Source/cmLocalGenerator.cxx b/Source/cmLocalGenerator.cxx index 4aecb1d..434cb10 100644 --- a/Source/cmLocalGenerator.cxx +++ b/Source/cmLocalGenerator.cxx @@ -26,7 +26,7 @@ #if defined(CMAKE_BUILD_WITH_CMAKE) #define CM_LG_ENCODE_OBJECT_NAMES -#include +#include "cmCryptoHash.h" #endif #include @@ -2001,17 +2001,6 @@ void cmLocalGenerator::GenerateTargetInstallRules( } #if defined(CM_LG_ENCODE_OBJECT_NAMES) -static std::string cmLocalGeneratorMD5(const char* input) -{ - char md5out[32]; - cmsysMD5* md5 = cmsysMD5_New(); - cmsysMD5_Initialize(md5); - cmsysMD5_Append(md5, reinterpret_cast(input), -1); - cmsysMD5_FinalizeHex(md5, md5out); - cmsysMD5_Delete(md5); - return std::string(md5out, 32); -} - static bool cmLocalGeneratorShortenObjectName(std::string& objName, std::string::size_type max_len) { @@ -2020,7 +2009,8 @@ static bool cmLocalGeneratorShortenObjectName(std::string& objName, std::string::size_type pos = objName.find('/', objName.size() - max_len + 32); if (pos != objName.npos) { - std::string md5name = cmLocalGeneratorMD5(objName.substr(0, pos).c_str()); + cmCryptoHash md5(cmCryptoHash::AlgoMD5); + std::string md5name = md5.HashString(objName.substr(0, pos)); md5name += objName.substr(pos); objName = md5name; diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 27fecdf..7738ab6 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -847,8 +847,8 @@ bool cmSystemTools::RenameFile(const char* oldname, const char* newname) bool cmSystemTools::ComputeFileMD5(const std::string& source, char* md5out) { #if defined(CMAKE_BUILD_WITH_CMAKE) - CM_AUTO_PTR md5 = cmCryptoHash::New("MD5"); - std::string str = md5->HashFile(source); + cmCryptoHash md5(cmCryptoHash::AlgoMD5); + std::string const str = md5.HashFile(source); strncpy(md5out, str.c_str(), 32); return !str.empty(); #else @@ -863,8 +863,8 @@ bool cmSystemTools::ComputeFileMD5(const std::string& source, char* md5out) std::string cmSystemTools::ComputeStringMD5(const std::string& input) { #if defined(CMAKE_BUILD_WITH_CMAKE) - CM_AUTO_PTR md5 = cmCryptoHash::New("MD5"); - return md5->HashString(input); + cmCryptoHash md5(cmCryptoHash::AlgoMD5); + return md5.HashString(input); #else (void)input; cmSystemTools::Message("md5sum not supported in bootstrapping mode", diff --git a/Source/cmUuid.cxx b/Source/cmUuid.cxx index 904bcbb..201e1cc 100644 --- a/Source/cmUuid.cxx +++ b/Source/cmUuid.cxx @@ -2,9 +2,8 @@ file Copyright.txt or https://cmake.org/licensing for details. */ #include "cmUuid.h" -#include "cm_sha2.h" +#include "cmCryptoHash.h" -#include #include cmUuid::cmUuid() @@ -22,16 +21,12 @@ std::string cmUuid::FromMd5(std::vector const& uuidNamespace, std::vector hashInput; this->CreateHashInput(uuidNamespace, name, hashInput); - cmsysMD5_s* md5 = cmsysMD5_New(); - cmsysMD5_Initialize(md5); - cmsysMD5_Append(md5, &hashInput[0], int(hashInput.size())); + cmCryptoHash md5(cmCryptoHash::AlgoMD5); + md5.Initialize(); + md5.Append(&hashInput[0], hashInput.size()); + std::vector digest = md5.Finalize(); - unsigned char digest[16] = { 0 }; - cmsysMD5_Finalize(md5, digest); - - cmsysMD5_Delete(md5); - - return this->FromDigest(digest, 3); + return this->FromDigest(&digest[0], 3); } std::string cmUuid::FromSha1(std::vector const& uuidNamespace, @@ -40,16 +35,12 @@ std::string cmUuid::FromSha1(std::vector const& uuidNamespace, std::vector hashInput; this->CreateHashInput(uuidNamespace, name, hashInput); - SHA_CTX* sha = new SHA_CTX; - SHA1_Init(sha); - SHA1_Update(sha, &hashInput[0], hashInput.size()); - - unsigned char digest[SHA1_DIGEST_LENGTH] = { 0 }; - SHA1_Final(digest, sha); - - delete sha; + cmCryptoHash sha1(cmCryptoHash::AlgoSHA1); + sha1.Initialize(); + sha1.Append(&hashInput[0], hashInput.size()); + std::vector digest = sha1.Finalize(); - return this->FromDigest(digest, 5); + return this->FromDigest(&digest[0], 5); } void cmUuid::CreateHashInput(std::vector const& uuidNamespace, -- cgit v0.12