Commit de53eb18 authored by Brad King's avatar Brad King Committed by Kitware Robot

Merge topic 'import-librhash'

cd8a57ae Add option to build CMake against a system librhash
71180fc8 FindLibRHash: Add module to find the librhash package
3216e94c Remove unused cm_sha2 infrastructure
5420278d Port hash computation to cmCryptoHash
9a596b33 cmCryptoHash: Re-implement in terms of librhash
47f91a61 cmCryptoHash: Avoid using subclasses at client sites
d0ff3e70 librhash: Port to KWIML for ABI and integer type information
465a85fb librhash: Avoid signed left-shift overflow
fc2cb74f librhash: Implement bswap_32 as a function even in strict C90 mode
0bd333bc librhash: Implement bswap_64 even in strict C90 mode
7189d62c librhash: Use __builtin_bswap{32,64} on Clang
af7ebf8a librhash: Install COPYING file with CMake documentation
bb01f20e librhash: Disable warnings to avoid changing 3rd party code
31bb727f librhash: Build the library within CMake
53048afa librhash: Remove source fragments not needed for CMake
5cb1b345 Merge branch 'upstream-librhash' into import-librhash
...
parents 25a76df1 cd8a57ae
Pipeline #34593 passed with stage
in 0 seconds
......@@ -104,7 +104,7 @@ macro(CMAKE_HANDLE_SYSTEM_LIBRARIES)
# Allow the user to enable/disable all system utility library options by
# defining CMAKE_USE_SYSTEM_LIBRARIES or CMAKE_USE_SYSTEM_LIBRARY_${util}.
set(UTILITIES BZIP2 CURL EXPAT FORM JSONCPP LIBARCHIVE LIBLZMA LIBUV ZLIB)
set(UTILITIES BZIP2 CURL EXPAT FORM JSONCPP LIBARCHIVE LIBLZMA LIBRHASH LIBUV ZLIB)
foreach(util ${UTILITIES})
if(NOT DEFINED CMAKE_USE_SYSTEM_LIBRARY_${util}
AND DEFINED CMAKE_USE_SYSTEM_LIBRARIES)
......@@ -144,6 +144,7 @@ macro(CMAKE_HANDLE_SYSTEM_LIBRARIES)
"${CMAKE_USE_SYSTEM_LIBRARY_LIBLZMA}" "NOT CMAKE_USE_SYSTEM_LIBARCHIVE" ON)
option(CMAKE_USE_SYSTEM_FORM "Use system-installed libform" "${CMAKE_USE_SYSTEM_LIBRARY_FORM}")
option(CMAKE_USE_SYSTEM_JSONCPP "Use system-installed jsoncpp" "${CMAKE_USE_SYSTEM_LIBRARY_JSONCPP}")
option(CMAKE_USE_SYSTEM_LIBRHASH "Use system-installed librhash" "${CMAKE_USE_SYSTEM_LIBRARY_LIBRHASH}")
option(CMAKE_USE_SYSTEM_LIBUV "Use system-installed libuv" "${CMAKE_USE_SYSTEM_LIBRARY_LIBUV}")
# For now use system KWIML only if explicitly requested rather
......@@ -298,6 +299,23 @@ macro (CMAKE_BUILD_UTILITIES)
add_subdirectory(Utilities/KWIML)
endif()
if(CMAKE_USE_SYSTEM_LIBRHASH)
if(NOT CMAKE_VERSION VERSION_LESS 3.0)
find_package(LibRHash)
else()
message(FATAL_ERROR "CMAKE_USE_SYSTEM_LIBRHASH requires CMake >= 3.0")
endif()
if(NOT LibRHash_FOUND)
message(FATAL_ERROR
"CMAKE_USE_SYSTEM_LIBRHASH is ON but LibRHash is not found!")
endif()
set(CMAKE_LIBRHASH_LIBRARIES LibRHash::LibRHash)
else()
set(CMAKE_LIBRHASH_LIBRARIES cmlibrhash)
add_subdirectory(Utilities/cmlibrhash)
CMAKE_SET_TARGET_FOLDER(cmlibrhash "Utilities/3rdParty")
endif()
#---------------------------------------------------------------------
# Build zlib library for Curl, CMake, and CTest.
set(CMAKE_ZLIB_HEADER "cm_zlib.h")
......
......@@ -17,7 +17,6 @@ list(APPEND CTEST_CUSTOM_WARNING_EXCEPTION
"Utilities.cmbzip2."
"Source.CTest.Curl"
"Source.CursesDialog.form"
"Source.cm_sha2.*warning.*cast increases required alignment of target type"
"Utilities.cmcurl"
"Utilities.cmexpat."
"Utilities.cmlibarchive"
......@@ -84,7 +83,6 @@ list(APPEND CTEST_CUSTOM_WARNING_EXCEPTION
"warning: Value stored to 'yytoken' is never read"
"index_encoder.c.241.2. warning: Value stored to .out_start. is never read"
"index.c.*warning: Access to field.*results in a dereference of a null pointer.*loaded from variable.*"
"cm_sha2.*warning: Value stored to.*is never read"
"cmFortranLexer.cxx:[0-9]+:[0-9]+: warning: Call to 'realloc' has an allocation size of 0 bytes"
"testProcess.*warning: Dereference of null pointer .loaded from variable .invalidAddress.."
"liblzma/simple/x86.c:[0-9]+:[0-9]+: warning: The result of the '<<' expression is undefined"
......
# Preserve upstream indentation style.
cm_sha2.* whitespace=indent-with-non-tab
# Preserve indentation style in generated code.
cmListFileLexer.c whitespace=-tab-in-indent,-indent-with-non-tab
cmFortranLexer.cxx whitespace=-tab-in-indent,-indent-with-non-tab
......
......@@ -626,8 +626,6 @@ set(SRCS
cm_auto_ptr.hxx
cm_get_date.h
cm_get_date.c
cm_sha2.h
cm_sha2.c
cm_utf8.h
cm_utf8.c
cm_codecvt.hxx
......@@ -788,6 +786,7 @@ target_link_libraries(CMakeLib cmsys
${CMAKE_CURL_LIBRARIES}
${CMAKE_JSONCPP_LIBRARIES}
${CMAKE_LIBUV_LIBRARIES}
${CMAKE_LIBRHASH_LIBRARIES}
${CMake_KWIML_LIBRARIES}
)
......
......@@ -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;
......
# Distributed under the OSI-approved BSD 3-Clause License. See accompanying
# file Copyright.txt or https://cmake.org/licensing for details.
#[=======================================================================[.rst:
FindLibRHash
------------
Find LibRHash include directory and library.
Imported Targets
^^^^^^^^^^^^^^^^
An :ref:`imported target <Imported targets>` named
``LibRHash::LibRHash`` is provided if LibRHash has been found.
Result Variables
^^^^^^^^^^^^^^^^
This module defines the following variables:
``LibRHash_FOUND``
True if LibRHash was found, false otherwise.
``LibRHash_INCLUDE_DIRS``
Include directories needed to include LibRHash headers.
``LibRHash_LIBRARIES``
Libraries needed to link to LibRHash.
Cache Variables
^^^^^^^^^^^^^^^
This module uses the following cache variables:
``LibRHash_LIBRARY``
The location of the LibRHash library file.
``LibRHash_INCLUDE_DIR``
The location of the LibRHash include directory containing ``rhash.h``.
The cache variables should not be used by project code.
They may be set by end users to point at LibRHash components.
#]=======================================================================]
#-----------------------------------------------------------------------------
find_library(LibRHash_LIBRARY
NAMES rhash
)
mark_as_advanced(LibRHash_LIBRARY)
find_path(LibRHash_INCLUDE_DIR
NAMES rhash.h
)
mark_as_advanced(LibRHash_INCLUDE_DIR)
#-----------------------------------------------------------------------------
include(${CMAKE_CURRENT_LIST_DIR}/../../Modules/FindPackageHandleStandardArgs.cmake)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(LibRHash
FOUND_VAR LibRHash_FOUND
REQUIRED_VARS LibRHash_LIBRARY LibRHash_INCLUDE_DIR
)
set(LIBRHASH_FOUND ${LibRHash_FOUND})
#-----------------------------------------------------------------------------
# Provide documented result variables and targets.
if(LibRHash_FOUND)
set(LibRHash_INCLUDE_DIRS ${LibRHash_INCLUDE_DIR})
set(LibRHash_LIBRARIES ${LibRHash_LIBRARY})
if(NOT TARGET LibRHash::LibRHash)
add_library(LibRHash::LibRHash UNKNOWN IMPORTED)
set_target_properties(LibRHash::LibRHash PROPERTIES
IMPORTED_LOCATION "${LibRHash_LIBRARY}"
INTERFACE_INCLUDE_DIRECTORIES "${LibRHash_INCLUDE_DIRS}"
)
endif()
endif()
......@@ -2,31 +2,62 @@
file Copyright.txt or https://cmake.org/licensing for details. */
#include "cmCryptoHash.h"
#include "cm_sha2.h"
#include <cm_kwiml.h>
#include <cm_rhash.h>
#include <cmsys/FStream.hxx>
#include <cmsys/MD5.h>
#include <string.h>
static unsigned int const cmCryptoHashAlgoToId[] = {
/* clang-format needs this comment to break after the opening brace */
RHASH_MD5, //
RHASH_SHA1, //
RHASH_SHA224, //
RHASH_SHA256, //
RHASH_SHA384, //
RHASH_SHA512
};
static int cmCryptoHash_rhash_library_initialized;
static rhash cmCryptoHash_rhash_init(unsigned int id)
{
if (!cmCryptoHash_rhash_library_initialized) {
cmCryptoHash_rhash_library_initialized = 1;
rhash_library_init();
}
return rhash_init(id);
}
cmCryptoHash::cmCryptoHash(Algo algo)
: Id(cmCryptoHashAlgoToId[algo])
, CTX(cmCryptoHash_rhash_init(Id))
{
}
cmCryptoHash::~cmCryptoHash()
{
rhash_free(this->CTX);
}
CM_AUTO_PTR<cmCryptoHash> cmCryptoHash::New(const char* algo)
{
if (strcmp(algo, "MD5") == 0) {
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashMD5);
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHash(AlgoMD5));
}
if (strcmp(algo, "SHA1") == 0) {
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA1);
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHash(AlgoSHA1));
}
if (strcmp(algo, "SHA224") == 0) {
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA224);
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHash(AlgoSHA224));
}
if (strcmp(algo, "SHA256") == 0) {
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA256);
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHash(AlgoSHA256));
}
if (strcmp(algo, "SHA384") == 0) {
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA384);
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHash(AlgoSHA384));
}
if (strcmp(algo, "SHA512") == 0) {
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA512);
return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHash(AlgoSHA512));
}
return CM_AUTO_PTR<cmCryptoHash>(CM_NULLPTR);
}
......@@ -80,7 +111,7 @@ std::vector<unsigned char> cmCryptoHash::ByteHashFile(const std::string& file)
this->Initialize();
{
// Should be efficient enough on most system:
cm_sha2_uint64_t buffer[512];
KWIML_INT_uint64_t buffer[512];
char* buffer_c = reinterpret_cast<char*>(buffer);
unsigned char const* buffer_uc =
reinterpret_cast<unsigned char const*>(buffer);
......@@ -117,51 +148,29 @@ std::string cmCryptoHash::HashFile(const std::string& file)
return ByteHashToString(this->ByteHashFile(file));
}
cmCryptoHashMD5::cmCryptoHashMD5()
: MD5(cmsysMD5_New())
void cmCryptoHash::Initialize()
{
rhash_reset(this->CTX);
}
cmCryptoHashMD5::~cmCryptoHashMD5()
void cmCryptoHash::Append(void const* buf, size_t sz)
{
cmsysMD5_Delete(this->MD5);
rhash_update(this->CTX, buf, sz);
}
void cmCryptoHashMD5::Initialize()
void cmCryptoHash::Append(std::string const& str)
{
cmsysMD5_Initialize(this->MD5);
this->Append(str.c_str(), str.size());
}
void cmCryptoHashMD5::Append(unsigned char const* buf, int sz)
std::vector<unsigned char> cmCryptoHash::Finalize()
{
cmsysMD5_Append(this->MD5, buf, sz);
std::vector<unsigned char> hash(rhash_get_digest_size(this->Id), 0);
rhash_final(this->CTX, &hash[0]);
return hash;
}
std::vector<unsigned char> cmCryptoHashMD5::Finalize()
std::string cmCryptoHash::FinalizeHex()
{
std::vector<unsigned char> hash(16, 0);
cmsysMD5_Finalize(this->MD5, &hash[0]);
return hash;
return cmCryptoHash::ByteHashToString(this->Finalize());
}
#define cmCryptoHash_SHA_CLASS_IMPL(SHA) \
cmCryptoHash##SHA::cmCryptoHash##SHA() \
: SHA(new SHA_CTX) \
{ \
} \
cmCryptoHash##SHA::~cmCryptoHash##SHA() { delete this->SHA; } \
void cmCryptoHash##SHA::Initialize() { SHA##_Init(this->SHA); } \
void cmCryptoHash##SHA::Append(unsigned char const* buf, int sz) \
{ \
SHA##_Update(this->SHA, buf, sz); \
} \
std::vector<unsigned char> cmCryptoHash##SHA::Finalize() \
{ \
std::vector<unsigned char> hash(SHA##_DIGEST_LENGTH, 0); \
SHA##_Final(&hash[0], this->SHA); \
return hash; \
}
cmCryptoHash_SHA_CLASS_IMPL(SHA1) cmCryptoHash_SHA_CLASS_IMPL(SHA224)
cmCryptoHash_SHA_CLASS_IMPL(SHA256) cmCryptoHash_SHA_CLASS_IMPL(SHA384)
cmCryptoHash_SHA_CLASS_IMPL(SHA512)
......@@ -15,7 +15,18 @@
class cmCryptoHash
{
public:
virtual ~cmCryptoHash() {}
enum Algo
{
AlgoMD5,
AlgoSHA1,
AlgoSHA224,
AlgoSHA256,
AlgoSHA384,
AlgoSHA512
};
cmCryptoHash(Algo algo);
~cmCryptoHash();
/// @brief Returns a new hash generator of the requested type
/// @arg algo Hash type name. Supported hash types are
......@@ -53,47 +64,15 @@ public:
/// An empty string otherwise.
std::string HashFile(const std::string& file);
protected:
virtual void Initialize() = 0;
virtual void Append(unsigned char const*, int) = 0;
virtual std::vector<unsigned char> Finalize() = 0;
};
class cmCryptoHashMD5 : public cmCryptoHash
{
struct cmsysMD5_s* MD5;
public:
cmCryptoHashMD5();
~cmCryptoHashMD5() CM_OVERRIDE;
void Initialize();
void Append(void const*, size_t);
void Append(std::string const& str);
std::vector<unsigned char> Finalize();
std::string FinalizeHex();
protected:
void Initialize() CM_OVERRIDE;
void Append(unsigned char const* buf, int sz) CM_OVERRIDE;
std::vector<unsigned char> Finalize() CM_OVERRIDE;
private:
unsigned int Id;
struct rhash_context* CTX;
};
#define cmCryptoHash_SHA_CLASS_DECL(SHA) \
class cmCryptoHash##SHA : public cmCryptoHash \
{ \
union _SHA_CTX* SHA; \
\
public: \
cmCryptoHash##SHA(); \
~cmCryptoHash##SHA(); \
\
protected: \
virtual void Initialize(); \
virtual void Append(unsigned char const* buf, int sz); \
virtual std::vector<unsigned char> Finalize(); \
}
cmCryptoHash_SHA_CLASS_DECL(SHA1);
cmCryptoHash_SHA_CLASS_DECL(SHA224);
cmCryptoHash_SHA_CLASS_DECL(SHA256);
cmCryptoHash_SHA_CLASS_DECL(SHA384);
cmCryptoHash_SHA_CLASS_DECL(SHA512);
#undef cmCryptoHash_SHA_CLASS_DECL
#endif
......@@ -2554,7 +2554,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);
......
......@@ -45,9 +45,9 @@
#include "cmake.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;
......@@ -2617,14 +2617,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).
......
......@@ -27,7 +27,7 @@
#if defined(CMAKE_BUILD_WITH_CMAKE)
#define CM_LG_ENCODE_OBJECT_NAMES
#include <cmsys/MD5.h>
#include "cmCryptoHash.h"
#endif
#include <algorithm>
......@@ -2002,17 +2002,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)
{
......@@ -2021,7 +2010,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)
cmCryptoHashMD5 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,7 +863,7 @@ bool cmSystemTools::ComputeFileMD5(const std::string& source, char* md5out)
std::string cmSystemTools::ComputeStringMD5(const std::string& input)
{
#if defined(CMAKE_BUILD_WITH_CMAKE)
cmCryptoHashMD5 md5;
cmCryptoHash md5(cmCryptoHash::AlgoMD5);
return md5.HashString(input);
#else
(void)input;
......
......@@ -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,
......
This diff is collapsed.
/*
* FILE: sha2.h
* AUTHOR: Aaron D. Gifford
* http://www.aarongifford.com/computers/sha.html
*
* Copyright (c) 2000-2003, Aaron D. Gifford
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTOR(S) ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTOR(S) BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* $Id: sha2.h,v 1.4 2004/01/07 19:06:18 adg Exp $
*/
#ifndef __SHA2_H__
#define __SHA2_H__
#include "cm_sha2_mangle.h"
/* CMake modification: use integer types from KWIML. */
#include <cm_kwiml.h>
typedef KWIML_INT_uint8_t cm_sha2_uint8_t;
typedef KWIML_INT_uint32_t cm_sha2_uint32_t;
typedef KWIML_INT_uint64_t cm_sha2_uint64_t;
#ifdef __cplusplus
extern "C" {
#endif
/*
* Import u_intXX_t size_t type definitions from system headers. You
* may need to change this, or define these things yourself in this
* file.
*/
#include <sys/types.h>
/*** SHA-224/256/384/512 Various Length Definitions *******************/
/* Digest lengths for SHA-1/224/256/384/512 */
#define SHA1_DIGEST_LENGTH 20
#define SHA1_DIGEST_STRING_LENGTH (SHA1_DIGEST_LENGTH * 2 + 1)
#define SHA224_DIGEST_LENGTH 28
#define SHA224_DIGEST_STRING_LENGTH (SHA224_DIGEST_LENGTH * 2 + 1)
#define SHA256_DIGEST_LENGTH 32
#define SHA256_DIGEST_STRING_LENGTH (SHA256_DIGEST_LENGTH * 2 + 1)
#define SHA384_DIGEST_LENGTH 48
#define SHA384_DIGEST_STRING_LENGTH (SHA384_DIGEST_LENGTH * 2 + 1)
#define SHA512_DIGEST_LENGTH 64
#define SHA512_DIGEST_STRING_LENGTH (SHA512_DIGEST_LENGTH * 2 + 1)
/*** SHA-224/256/384/512 Context Structures ***************************/
typedef union _SHA_CTX {
/* SHA-1 uses this part of the union: */
struct {
cm_sha2_uint32_t state[5];
cm_sha2_uint64_t bitcount;
cm_sha2_uint8_t buffer[64];
} s1;
/* SHA-224 and SHA-256 use this part of the union: */
struct {
cm_sha2_uint32_t state[8];
cm_sha2_uint64_t bitcount;
cm_sha2_uint8_t buffer[64];
} s256;
/* SHA-384 and SHA-512 use this part of the union: */
struct {
cm_sha2_uint64_t state[8];
cm_sha2_uint64_t bitcount[2];
cm_sha2_uint8_t buffer[128];
} s512;
} SHA_CTX;
/*** SHA-256/384/512 Function Prototypes ******************************/
void SHA1_Init(SHA_CTX*);
void SHA1_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA1_Final(cm_sha2_uint8_t[SHA1_DIGEST_LENGTH], SHA_CTX*);
char* SHA1_End(SHA_CTX*, char[SHA1_DIGEST_STRING_LENGTH]);
char* SHA1_Data(const cm_sha2_uint8_t*, size_t,
char[SHA1_DIGEST_STRING_LENGTH]);
void SHA224_Init(SHA_CTX*);
void SHA224_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA224_Final(cm_sha2_uint8_t[SHA224_DIGEST_LENGTH], SHA_CTX*);
char* SHA224_End(SHA_CTX*, char[SHA224_DIGEST_STRING_LENGTH]);
char* SHA224_Data(const cm_sha2_uint8_t*, size_t,
char[SHA224_DIGEST_STRING_LENGTH]);
void SHA256_Init(SHA_CTX*);
void SHA256_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA256_Final(cm_sha2_uint8_t[SHA256_DIGEST_LENGTH], SHA_CTX*);
char* SHA256_End(SHA_CTX*, char[SHA256_DIGEST_STRING_LENGTH]);
char* SHA256_Data(const cm_sha2_uint8_t*, size_t,
char[SHA256_DIGEST_STRING_LENGTH]);
void SHA384_Init(SHA_CTX*);
void SHA384_Update(SHA_CTX*, const cm_sha2_uint8_t*, size_t);
void SHA384_Final(cm_sha2_uint8_t[SHA384_DIGEST_LENGTH], SHA_CTX*);