summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBrad King <brad.king@kitware.com>2016-11-03 17:21:41 (GMT)
committerBrad King <brad.king@kitware.com>2016-11-10 13:29:38 (GMT)
commit5420278dc884c0382f271872b67c33978f3fe6b8 (patch)
treebbc69e42b2785521157da63324473b0f0d36fb64
parent9a596b33bbfdb274ccf7f678c78cb8826c7c363b (diff)
downloadCMake-5420278dc884c0382f271872b67c33978f3fe6b8.zip
CMake-5420278dc884c0382f271872b67c33978f3fe6b8.tar.gz
CMake-5420278dc884c0382f271872b67c33978f3fe6b8.tar.bz2
Port hash computation to cmCryptoHash
Avoid using KWSys MD5 or `cm_sha2` and use the `cmCryptoHash` abstraction instead.
-rw-r--r--Source/CPack/WiX/cmCPackWIXGenerator.cxx4
-rw-r--r--Source/CTest/cmCTestLaunch.cxx15
-rw-r--r--Source/cmFileCommand.cxx3
-rw-r--r--Source/cmFilePathUuid.cxx4
-rw-r--r--Source/cmGlobalGenerator.cxx13
-rw-r--r--Source/cmLocalGenerator.cxx16
-rw-r--r--Source/cmSystemTools.cxx8
-rw-r--r--Source/cmUuid.cxx31
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<cmCryptoHash> 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 <cmConfigure.h>
+#include "cmCryptoHash.h"
#include "cmGeneratedFileStream.h"
#include "cmGlobalGenerator.h"
#include "cmMakefile.h"
@@ -14,7 +15,6 @@
#include <cm_auto_ptr.hxx>
#include <cmsys/FStream.hxx>
-#include <cmsys/MD5.h>
#include <cmsys/Process.h>
#include <cmsys/RegularExpression.hxx>
#include <iostream>
@@ -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<std::string>::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<std::string> const& args)
this->SetError("DOWNLOAD missing sum value for EXPECTED_MD5.");
return false;
}
- hash = CM_AUTO_PTR<cmCryptoHash>(cmCryptoHash::New("MD5"));
+ hash =
+ CM_AUTO_PTR<cmCryptoHash>(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<unsigned char> 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 <string.h>
#if defined(CMAKE_BUILD_WITH_CMAKE)
+#include "cmCryptoHash.h"
#include <cm_jsoncpp_value.h>
#include <cm_jsoncpp_writer.h>
-#include <cmsys/MD5.h>
#endif
class cmInstalledFile;
@@ -2616,14 +2616,9 @@ void cmGlobalGenerator::AddRuleHash(const std::vector<std::string>& outputs,
// Compute a hash of the rule.
RuleHash hash;
{
- unsigned char const* data =
- reinterpret_cast<unsigned char const*>(content.c_str());
- int length = static_cast<int>(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 <cmsys/MD5.h>
+#include "cmCryptoHash.h"
#endif
#include <algorithm>
@@ -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<unsigned char const*>(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<cmCryptoHash> 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<cmCryptoHash> 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 <cmsys/MD5.h>
#include <string.h>
cmUuid::cmUuid()
@@ -22,16 +21,12 @@ std::string cmUuid::FromMd5(std::vector<unsigned char> const& uuidNamespace,
std::vector<unsigned char> 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<unsigned char> 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<unsigned char> const& uuidNamespace,
@@ -40,16 +35,12 @@ std::string cmUuid::FromSha1(std::vector<unsigned char> const& uuidNamespace,
std::vector<unsigned char> 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<unsigned char> digest = sha1.Finalize();
- return this->FromDigest(digest, 5);
+ return this->FromDigest(&digest[0], 5);
}
void cmUuid::CreateHashInput(std::vector<unsigned char> const& uuidNamespace,