cmCryptoHash.cxx 5.6 KB

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