mirror of
https://github.com/reactos/CMake.git
synced 2025-02-27 07:27:23 +00:00
cmCryptoHash: Accept cm::string_view input
This commit is contained in:
parent
34301441b9
commit
f1c529c4fb
@ -6,8 +6,6 @@
|
||||
#include "cm_rhash.h"
|
||||
#include "cmsys/FStream.hxx"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "cm_memory.hxx"
|
||||
|
||||
static unsigned int const cmCryptoHashAlgoToId[] = {
|
||||
@ -46,36 +44,36 @@ cmCryptoHash::~cmCryptoHash()
|
||||
rhash_free(this->CTX);
|
||||
}
|
||||
|
||||
std::unique_ptr<cmCryptoHash> cmCryptoHash::New(const char* algo)
|
||||
std::unique_ptr<cmCryptoHash> cmCryptoHash::New(cm::string_view algo)
|
||||
{
|
||||
if (strcmp(algo, "MD5") == 0) {
|
||||
if (algo == "MD5") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoMD5);
|
||||
}
|
||||
if (strcmp(algo, "SHA1") == 0) {
|
||||
if (algo == "SHA1") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA1);
|
||||
}
|
||||
if (strcmp(algo, "SHA224") == 0) {
|
||||
if (algo == "SHA224") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA224);
|
||||
}
|
||||
if (strcmp(algo, "SHA256") == 0) {
|
||||
if (algo == "SHA256") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA256);
|
||||
}
|
||||
if (strcmp(algo, "SHA384") == 0) {
|
||||
if (algo == "SHA384") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA384);
|
||||
}
|
||||
if (strcmp(algo, "SHA512") == 0) {
|
||||
if (algo == "SHA512") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA512);
|
||||
}
|
||||
if (strcmp(algo, "SHA3_224") == 0) {
|
||||
if (algo == "SHA3_224") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA3_224);
|
||||
}
|
||||
if (strcmp(algo, "SHA3_256") == 0) {
|
||||
if (algo == "SHA3_256") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA3_256);
|
||||
}
|
||||
if (strcmp(algo, "SHA3_384") == 0) {
|
||||
if (algo == "SHA3_384") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA3_384);
|
||||
}
|
||||
if (strcmp(algo, "SHA3_512") == 0) {
|
||||
if (algo == "SHA3_512") {
|
||||
return cm::make_unique<cmCryptoHash>(AlgoSHA3_512);
|
||||
}
|
||||
return std::unique_ptr<cmCryptoHash>(nullptr);
|
||||
@ -106,6 +104,7 @@ std::string cmCryptoHash::ByteHashToString(
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
|
||||
std::string res;
|
||||
res.reserve(hash.size() * 2);
|
||||
for (unsigned char v : hash) {
|
||||
res.push_back(hex[v >> 4]);
|
||||
res.push_back(hex[v & 0xF]);
|
||||
@ -113,12 +112,10 @@ std::string cmCryptoHash::ByteHashToString(
|
||||
return res;
|
||||
}
|
||||
|
||||
std::vector<unsigned char> cmCryptoHash::ByteHashString(
|
||||
const std::string& input)
|
||||
std::vector<unsigned char> cmCryptoHash::ByteHashString(cm::string_view input)
|
||||
{
|
||||
this->Initialize();
|
||||
this->Append(reinterpret_cast<unsigned char const*>(input.c_str()),
|
||||
static_cast<int>(input.size()));
|
||||
this->Append(input);
|
||||
return this->Finalize();
|
||||
}
|
||||
|
||||
@ -156,7 +153,7 @@ std::vector<unsigned char> cmCryptoHash::ByteHashFile(const std::string& file)
|
||||
return std::vector<unsigned char>();
|
||||
}
|
||||
|
||||
std::string cmCryptoHash::HashString(const std::string& input)
|
||||
std::string cmCryptoHash::HashString(cm::string_view input)
|
||||
{
|
||||
return ByteHashToString(this->ByteHashString(input));
|
||||
}
|
||||
@ -176,9 +173,9 @@ void cmCryptoHash::Append(void const* buf, size_t sz)
|
||||
rhash_update(this->CTX, buf, sz);
|
||||
}
|
||||
|
||||
void cmCryptoHash::Append(std::string const& str)
|
||||
void cmCryptoHash::Append(cm::string_view input)
|
||||
{
|
||||
this->Append(str.c_str(), str.size());
|
||||
rhash_update(this->CTX, input.data(), input.size());
|
||||
}
|
||||
|
||||
std::vector<unsigned char> cmCryptoHash::Finalize()
|
||||
|
@ -5,6 +5,8 @@
|
||||
|
||||
#include "cmConfigure.h" // IWYU pragma: keep
|
||||
|
||||
#include "cm_string_view.hxx"
|
||||
|
||||
#include <memory>
|
||||
#include <stddef.h>
|
||||
#include <string>
|
||||
@ -42,7 +44,7 @@ public:
|
||||
/// SHA3_224, SHA3_256, SHA3_384, SHA3_512
|
||||
/// @return A valid auto pointer if algo is supported or
|
||||
/// an invalid/NULL pointer otherwise
|
||||
static std::unique_ptr<cmCryptoHash> New(const char* algo);
|
||||
static std::unique_ptr<cmCryptoHash> New(cm::string_view algo);
|
||||
|
||||
/// @brief Converts a hex character to its binary value (4 bits)
|
||||
/// @arg input Hex character [0-9a-fA-F].
|
||||
@ -55,7 +57,7 @@ public:
|
||||
|
||||
/// @brief Calculates a binary hash from string input data
|
||||
/// @return Binary hash vector
|
||||
std::vector<unsigned char> ByteHashString(const std::string& input);
|
||||
std::vector<unsigned char> ByteHashString(cm::string_view input);
|
||||
|
||||
/// @brief Calculates a binary hash from file content
|
||||
/// @see ByteHashString()
|
||||
@ -65,7 +67,7 @@ public:
|
||||
|
||||
/// @brief Calculates a hash string from string input data
|
||||
/// @return Sequence of hex characters pairs for each byte of the binary hash
|
||||
std::string HashString(const std::string& input);
|
||||
std::string HashString(cm::string_view input);
|
||||
|
||||
/// @brief Calculates a hash string from file content
|
||||
/// @see HashString()
|
||||
@ -75,7 +77,7 @@ public:
|
||||
|
||||
void Initialize();
|
||||
void Append(void const*, size_t);
|
||||
void Append(std::string const& str);
|
||||
void Append(cm::string_view input);
|
||||
std::vector<unsigned char> Finalize();
|
||||
std::string FinalizeHex();
|
||||
|
||||
|
@ -283,7 +283,7 @@ bool HandleHashCommand(std::vector<std::string> const& args,
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
|
||||
std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0]));
|
||||
if (hash) {
|
||||
std::string out = hash->HashFile(args[1]);
|
||||
if (!out.empty()) {
|
||||
@ -1712,7 +1712,7 @@ bool HandleDownloadCommand(std::vector<std::string> const& args,
|
||||
}
|
||||
std::string algo = i->substr(0, pos);
|
||||
expectedHash = cmSystemTools::LowerCase(i->substr(pos + 1));
|
||||
hash = std::unique_ptr<cmCryptoHash>(cmCryptoHash::New(algo.c_str()));
|
||||
hash = std::unique_ptr<cmCryptoHash>(cmCryptoHash::New(algo));
|
||||
if (!hash) {
|
||||
std::string err = "DOWNLOAD EXPECTED_HASH given unknown ALGO: ";
|
||||
err += algo;
|
||||
|
@ -121,7 +121,7 @@ bool cmStringCommand::HandleHashCommand(std::vector<std::string> const& args)
|
||||
return false;
|
||||
}
|
||||
|
||||
std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0].c_str()));
|
||||
std::unique_ptr<cmCryptoHash> hash(cmCryptoHash::New(args[0]));
|
||||
if (hash) {
|
||||
std::string out = hash->HashString(args[2]);
|
||||
this->Makefile->AddDefinition(args[1], out);
|
||||
|
Loading…
x
Reference in New Issue
Block a user