cmake/Source/cmCryptoHash.cxx

193 lines
4.9 KiB
C++
Raw Normal View History

2016-10-30 18:24:19 +01:00
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
2012-02-18 12:40:36 +02:00
#include "cmCryptoHash.h"
2020-02-01 23:06:01 +01:00
#include <cm/memory>
2020-08-30 11:54:41 +02:00
#include <cm3p/kwiml/int.h>
#include <cm3p/rhash.h>
2012-02-18 12:40:36 +02:00
2020-08-30 11:54:41 +02:00
#include "cmsys/FStream.hxx"
2018-01-26 17:06:56 +01:00
2017-04-14 19:02:05 +02:00
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, //
RHASH_SHA3_224, //
RHASH_SHA3_256, //
RHASH_SHA3_384, //
RHASH_SHA3_512
};
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])
2021-09-14 00:13:48 +02:00
, CTX(cmCryptoHash_rhash_init(this->Id))
2017-04-14 19:02:05 +02:00
{
}
cmCryptoHash::~cmCryptoHash()
{
rhash_free(this->CTX);
}
2020-02-01 23:06:01 +01:00
std::unique_ptr<cmCryptoHash> cmCryptoHash::New(cm::string_view algo)
2012-02-18 12:40:36 +02:00
{
2020-02-01 23:06:01 +01:00
if (algo == "MD5") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoMD5);
2016-10-30 18:24:19 +01:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA1") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA1);
2016-10-30 18:24:19 +01:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA224") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA224);
2016-07-09 11:21:54 +02:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA256") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA256);
2016-10-30 18:24:19 +01:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA384") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA384);
2016-10-30 18:24:19 +01:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA512") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA512);
2017-04-14 19:02:05 +02:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA3_224") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA3_224);
2017-04-14 19:02:05 +02:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA3_256") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA3_256);
2017-04-14 19:02:05 +02:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA3_384") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA3_384);
2017-04-14 19:02:05 +02:00
}
2020-02-01 23:06:01 +01:00
if (algo == "SHA3_512") {
2018-01-26 17:06:56 +01:00
return cm::make_unique<cmCryptoHash>(AlgoSHA3_512);
2016-10-30 18:24:19 +01:00
}
2018-01-26 17:06:56 +01:00
return std::unique_ptr<cmCryptoHash>(nullptr);
2012-02-18 12:40:36 +02:00
}
2016-10-30 18:24:19 +01:00
bool cmCryptoHash::IntFromHexDigit(char input, char& output)
{
if (input >= '0' && input <= '9') {
2022-08-04 22:12:04 +02:00
output = static_cast<char>(input - '0');
2016-10-30 18:24:19 +01:00
return true;
}
if (input >= 'a' && input <= 'f') {
2022-08-04 22:12:04 +02:00
output = static_cast<char>(input - 'a' + 0xA);
2016-10-30 18:24:19 +01:00
return true;
}
if (input >= 'A' && input <= 'F') {
2022-08-04 22:12:04 +02:00
output = static_cast<char>(input - 'A' + 0xA);
2016-10-30 18:24:19 +01:00
return true;
}
return false;
}
std::string cmCryptoHash::ByteHashToString(
const std::vector<unsigned char>& hash)
{
// Map from 4-bit index to hexadecimal representation.
static char const hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
std::string res;
2020-02-01 23:06:01 +01:00
res.reserve(hash.size() * 2);
2018-01-26 17:06:56 +01:00
for (unsigned char v : hash) {
res.push_back(hex[v >> 4]);
res.push_back(hex[v & 0xF]);
2016-10-30 18:24:19 +01:00
}
return res;
}
2020-02-01 23:06:01 +01:00
std::vector<unsigned char> cmCryptoHash::ByteHashString(cm::string_view input)
2012-02-18 12:40:36 +02:00
{
this->Initialize();
2020-02-01 23:06:01 +01:00
this->Append(input);
2012-02-18 12:40:36 +02:00
return this->Finalize();
}
2016-10-30 18:24:19 +01:00
std::vector<unsigned char> cmCryptoHash::ByteHashFile(const std::string& file)
2012-02-18 12:40:36 +02:00
{
2015-11-17 17:22:37 +01:00
cmsys::ifstream fin(file.c_str(), std::ios::in | std::ios::binary);
2016-10-30 18:24:19 +01:00
if (fin) {
this->Initialize();
{
// Should be efficient enough on most system:
2017-04-14 19:02:05 +02:00
KWIML_INT_uint64_t buffer[512];
2016-10-30 18:24:19 +01:00
char* buffer_c = reinterpret_cast<char*>(buffer);
unsigned char const* buffer_uc =
reinterpret_cast<unsigned char const*>(buffer);
// This copy loop is very sensitive on certain platforms with
// slightly broken stream libraries (like HPUX). Normally, it is
// incorrect to not check the error condition on the fin.read()
// before using the data, but the fin.gcount() will be zero if an
// error occurred. Therefore, the loop should be safe everywhere.
while (fin) {
fin.read(buffer_c, sizeof(buffer));
if (int gcount = static_cast<int>(fin.gcount())) {
this->Append(buffer_uc, gcount);
}
}
}
if (fin.eof()) {
// Success
return this->Finalize();
}
// Finalize anyway
this->Finalize();
2016-07-09 11:21:54 +02:00
}
2016-10-30 18:24:19 +01:00
// Return without success
return std::vector<unsigned char>();
}
2012-02-18 12:40:36 +02:00
2020-02-01 23:06:01 +01:00
std::string cmCryptoHash::HashString(cm::string_view input)
2016-10-30 18:24:19 +01:00
{
return ByteHashToString(this->ByteHashString(input));
}
2012-02-18 12:40:36 +02:00
2016-10-30 18:24:19 +01:00
std::string cmCryptoHash::HashFile(const std::string& file)
{
return ByteHashToString(this->ByteHashFile(file));
2012-02-18 12:40:36 +02:00
}
2017-04-14 19:02:05 +02:00
void cmCryptoHash::Initialize()
2012-02-18 12:40:36 +02:00
{
2017-04-14 19:02:05 +02:00
rhash_reset(this->CTX);
2012-02-18 12:40:36 +02:00
}
2017-04-14 19:02:05 +02:00
void cmCryptoHash::Append(void const* buf, size_t sz)
2012-02-18 12:40:36 +02:00
{
2017-04-14 19:02:05 +02:00
rhash_update(this->CTX, buf, sz);
2012-02-18 12:40:36 +02:00
}
2020-02-01 23:06:01 +01:00
void cmCryptoHash::Append(cm::string_view input)
2012-02-18 12:40:36 +02:00
{
2020-02-01 23:06:01 +01:00
rhash_update(this->CTX, input.data(), input.size());
2012-02-18 12:40:36 +02:00
}
2017-04-14 19:02:05 +02:00
std::vector<unsigned char> cmCryptoHash::Finalize()
2012-02-18 12:40:36 +02:00
{
2017-04-14 19:02:05 +02:00
std::vector<unsigned char> hash(rhash_get_digest_size(this->Id), 0);
2022-08-04 22:12:04 +02:00
rhash_final(this->CTX, hash.data());
2017-04-14 19:02:05 +02:00
return hash;
2012-02-18 12:40:36 +02:00
}
2017-04-14 19:02:05 +02:00
std::string cmCryptoHash::FinalizeHex()
2012-02-18 12:40:36 +02:00
{
2017-04-14 19:02:05 +02:00
return cmCryptoHash::ByteHashToString(this->Finalize());
2012-02-18 12:40:36 +02:00
}