Commit 5420278d authored by Brad King's avatar Brad King

Port hash computation to cmCryptoHash

Avoid using KWSys MD5 or `cm_sha2` and use the `cmCryptoHash`
abstraction instead.
parent 9a596b33
Pipeline #34580 passed with stage
......@@ -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) + "_";
......
......@@ -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;
......
......@@ -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") {
......
......@@ -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);
......
......@@ -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).
......
......@@ -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;
......
......@@ -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",
......
......@@ -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,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment