cmCryptoHash.cxx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmCryptoHash.h"
  4. #include "cmAlgorithms.h"
  5. #include "cm_kwiml.h"
  6. #include "cm_rhash.h"
  7. #include "cmsys/FStream.hxx"
  8. #include <string.h>
  9. #include <memory> // IWYU pragma: keep
  10. static unsigned int const cmCryptoHashAlgoToId[] = {
  11. /* clang-format needs this comment to break after the opening brace */
  12. RHASH_MD5, //
  13. RHASH_SHA1, //
  14. RHASH_SHA224, //
  15. RHASH_SHA256, //
  16. RHASH_SHA384, //
  17. RHASH_SHA512, //
  18. RHASH_SHA3_224, //
  19. RHASH_SHA3_256, //
  20. RHASH_SHA3_384, //
  21. RHASH_SHA3_512
  22. };
  23. static int cmCryptoHash_rhash_library_initialized;
  24. static rhash cmCryptoHash_rhash_init(unsigned int id)
  25. {
  26. if (!cmCryptoHash_rhash_library_initialized) {
  27. cmCryptoHash_rhash_library_initialized = 1;
  28. rhash_library_init();
  29. }
  30. return rhash_init(id);
  31. }
  32. cmCryptoHash::cmCryptoHash(Algo algo)
  33. : Id(cmCryptoHashAlgoToId[algo])
  34. , CTX(cmCryptoHash_rhash_init(Id))
  35. {
  36. }
  37. cmCryptoHash::~cmCryptoHash()
  38. {
  39. rhash_free(this->CTX);
  40. }
  41. std::unique_ptr<cmCryptoHash> cmCryptoHash::New(const char* algo)
  42. {
  43. if (strcmp(algo, "MD5") == 0) {
  44. return cm::make_unique<cmCryptoHash>(AlgoMD5);
  45. }
  46. if (strcmp(algo, "SHA1") == 0) {
  47. return cm::make_unique<cmCryptoHash>(AlgoSHA1);
  48. }
  49. if (strcmp(algo, "SHA224") == 0) {
  50. return cm::make_unique<cmCryptoHash>(AlgoSHA224);
  51. }
  52. if (strcmp(algo, "SHA256") == 0) {
  53. return cm::make_unique<cmCryptoHash>(AlgoSHA256);
  54. }
  55. if (strcmp(algo, "SHA384") == 0) {
  56. return cm::make_unique<cmCryptoHash>(AlgoSHA384);
  57. }
  58. if (strcmp(algo, "SHA512") == 0) {
  59. return cm::make_unique<cmCryptoHash>(AlgoSHA512);
  60. }
  61. if (strcmp(algo, "SHA3_224") == 0) {
  62. return cm::make_unique<cmCryptoHash>(AlgoSHA3_224);
  63. }
  64. if (strcmp(algo, "SHA3_256") == 0) {
  65. return cm::make_unique<cmCryptoHash>(AlgoSHA3_256);
  66. }
  67. if (strcmp(algo, "SHA3_384") == 0) {
  68. return cm::make_unique<cmCryptoHash>(AlgoSHA3_384);
  69. }
  70. if (strcmp(algo, "SHA3_512") == 0) {
  71. return cm::make_unique<cmCryptoHash>(AlgoSHA3_512);
  72. }
  73. return std::unique_ptr<cmCryptoHash>(nullptr);
  74. }
  75. bool cmCryptoHash::IntFromHexDigit(char input, char& output)
  76. {
  77. if (input >= '0' && input <= '9') {
  78. output = char(input - '0');
  79. return true;
  80. }
  81. if (input >= 'a' && input <= 'f') {
  82. output = char(input - 'a' + 0xA);
  83. return true;
  84. }
  85. if (input >= 'A' && input <= 'F') {
  86. output = char(input - 'A' + 0xA);
  87. return true;
  88. }
  89. return false;
  90. }
  91. std::string cmCryptoHash::ByteHashToString(
  92. const std::vector<unsigned char>& hash)
  93. {
  94. // Map from 4-bit index to hexadecimal representation.
  95. static char const hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
  96. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  97. std::string res;
  98. for (unsigned char v : hash) {
  99. res.push_back(hex[v >> 4]);
  100. res.push_back(hex[v & 0xF]);
  101. }
  102. return res;
  103. }
  104. std::vector<unsigned char> cmCryptoHash::ByteHashString(
  105. const std::string& input)
  106. {
  107. this->Initialize();
  108. this->Append(reinterpret_cast<unsigned char const*>(input.c_str()),
  109. static_cast<int>(input.size()));
  110. return this->Finalize();
  111. }
  112. std::vector<unsigned char> cmCryptoHash::ByteHashFile(const std::string& file)
  113. {
  114. cmsys::ifstream fin(file.c_str(), std::ios::in | std::ios::binary);
  115. if (fin) {
  116. this->Initialize();
  117. {
  118. // Should be efficient enough on most system:
  119. KWIML_INT_uint64_t buffer[512];
  120. char* buffer_c = reinterpret_cast<char*>(buffer);
  121. unsigned char const* buffer_uc =
  122. reinterpret_cast<unsigned char const*>(buffer);
  123. // This copy loop is very sensitive on certain platforms with
  124. // slightly broken stream libraries (like HPUX). Normally, it is
  125. // incorrect to not check the error condition on the fin.read()
  126. // before using the data, but the fin.gcount() will be zero if an
  127. // error occurred. Therefore, the loop should be safe everywhere.
  128. while (fin) {
  129. fin.read(buffer_c, sizeof(buffer));
  130. if (int gcount = static_cast<int>(fin.gcount())) {
  131. this->Append(buffer_uc, gcount);
  132. }
  133. }
  134. }
  135. if (fin.eof()) {
  136. // Success
  137. return this->Finalize();
  138. }
  139. // Finalize anyway
  140. this->Finalize();
  141. }
  142. // Return without success
  143. return std::vector<unsigned char>();
  144. }
  145. std::string cmCryptoHash::HashString(const std::string& input)
  146. {
  147. return ByteHashToString(this->ByteHashString(input));
  148. }
  149. std::string cmCryptoHash::HashFile(const std::string& file)
  150. {
  151. return ByteHashToString(this->ByteHashFile(file));
  152. }
  153. void cmCryptoHash::Initialize()
  154. {
  155. rhash_reset(this->CTX);
  156. }
  157. void cmCryptoHash::Append(void const* buf, size_t sz)
  158. {
  159. rhash_update(this->CTX, buf, sz);
  160. }
  161. void cmCryptoHash::Append(std::string const& str)
  162. {
  163. this->Append(str.c_str(), str.size());
  164. }
  165. std::vector<unsigned char> cmCryptoHash::Finalize()
  166. {
  167. std::vector<unsigned char> hash(rhash_get_digest_size(this->Id), 0);
  168. rhash_final(this->CTX, &hash[0]);
  169. return hash;
  170. }
  171. std::string cmCryptoHash::FinalizeHex()
  172. {
  173. return cmCryptoHash::ByteHashToString(this->Finalize());
  174. }