diff options
author | Sebastian Holtermann <sebholt@xwmw.org> | 2019-08-18 11:58:04 (GMT) |
---|---|---|
committer | Sebastian Holtermann <sebholt@xwmw.org> | 2019-08-18 12:17:50 (GMT) |
commit | f1c529c4fbea1963f7df92597a350e066319a1e1 (patch) | |
tree | a544103f96e0b03a8b659d4d5767360ec05ff1cf /Source/cmCryptoHash.cxx | |
parent | 34301441b963a2d7e02b4a4355a6634b3bbb7977 (diff) | |
download | CMake-f1c529c4fbea1963f7df92597a350e066319a1e1.zip CMake-f1c529c4fbea1963f7df92597a350e066319a1e1.tar.gz CMake-f1c529c4fbea1963f7df92597a350e066319a1e1.tar.bz2 |
cmCryptoHash: Accept cm::string_view input
Diffstat (limited to 'Source/cmCryptoHash.cxx')
-rw-r--r-- | Source/cmCryptoHash.cxx | 37 |
1 files changed, 17 insertions, 20 deletions
diff --git a/Source/cmCryptoHash.cxx b/Source/cmCryptoHash.cxx index 5e919af..f9f9581 100644 --- a/Source/cmCryptoHash.cxx +++ b/Source/cmCryptoHash.cxx @@ -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() |