cmCryptoHash.cxx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2009 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmCryptoHash.h"
  11. #include "cm_sha2.h"
  12. #include <cmsys/FStream.hxx>
  13. #include <cmsys/MD5.h>
  14. #include <string.h>
  15. CM_AUTO_PTR<cmCryptoHash> cmCryptoHash::New(const char* algo)
  16. {
  17. if (strcmp(algo, "MD5") == 0) {
  18. return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashMD5);
  19. }
  20. if (strcmp(algo, "SHA1") == 0) {
  21. return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA1);
  22. }
  23. if (strcmp(algo, "SHA224") == 0) {
  24. return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA224);
  25. }
  26. if (strcmp(algo, "SHA256") == 0) {
  27. return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA256);
  28. }
  29. if (strcmp(algo, "SHA384") == 0) {
  30. return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA384);
  31. }
  32. if (strcmp(algo, "SHA512") == 0) {
  33. return CM_AUTO_PTR<cmCryptoHash>(new cmCryptoHashSHA512);
  34. }
  35. return CM_AUTO_PTR<cmCryptoHash>(CM_NULLPTR);
  36. }
  37. bool cmCryptoHash::IntFromHexDigit(char input, char& output)
  38. {
  39. if (input >= '0' && input <= '9') {
  40. output = char(input - '0');
  41. return true;
  42. }
  43. if (input >= 'a' && input <= 'f') {
  44. output = char(input - 'a' + 0xA);
  45. return true;
  46. }
  47. if (input >= 'A' && input <= 'F') {
  48. output = char(input - 'A' + 0xA);
  49. return true;
  50. }
  51. return false;
  52. }
  53. std::string cmCryptoHash::ByteHashToString(
  54. const std::vector<unsigned char>& hash)
  55. {
  56. // Map from 4-bit index to hexadecimal representation.
  57. static char const hex[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
  58. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
  59. std::string res;
  60. for (std::vector<unsigned char>::const_iterator vit = hash.begin();
  61. vit != hash.end(); ++vit) {
  62. res.push_back(hex[(*vit) >> 4]);
  63. res.push_back(hex[(*vit) & 0xF]);
  64. }
  65. return res;
  66. }
  67. std::vector<unsigned char> cmCryptoHash::ByteHashString(
  68. const std::string& input)
  69. {
  70. this->Initialize();
  71. this->Append(reinterpret_cast<unsigned char const*>(input.c_str()),
  72. static_cast<int>(input.size()));
  73. return this->Finalize();
  74. }
  75. std::vector<unsigned char> cmCryptoHash::ByteHashFile(const std::string& file)
  76. {
  77. cmsys::ifstream fin(file.c_str(), std::ios::in | std::ios::binary);
  78. if (fin) {
  79. this->Initialize();
  80. {
  81. // Should be efficient enough on most system:
  82. cm_sha2_uint64_t buffer[512];
  83. char* buffer_c = reinterpret_cast<char*>(buffer);
  84. unsigned char const* buffer_uc =
  85. reinterpret_cast<unsigned char const*>(buffer);
  86. // This copy loop is very sensitive on certain platforms with
  87. // slightly broken stream libraries (like HPUX). Normally, it is
  88. // incorrect to not check the error condition on the fin.read()
  89. // before using the data, but the fin.gcount() will be zero if an
  90. // error occurred. Therefore, the loop should be safe everywhere.
  91. while (fin) {
  92. fin.read(buffer_c, sizeof(buffer));
  93. if (int gcount = static_cast<int>(fin.gcount())) {
  94. this->Append(buffer_uc, gcount);
  95. }
  96. }
  97. }
  98. if (fin.eof()) {
  99. // Success
  100. return this->Finalize();
  101. }
  102. // Finalize anyway
  103. this->Finalize();
  104. }
  105. // Return without success
  106. return std::vector<unsigned char>();
  107. }
  108. std::string cmCryptoHash::HashString(const std::string& input)
  109. {
  110. return ByteHashToString(this->ByteHashString(input));
  111. }
  112. std::string cmCryptoHash::HashFile(const std::string& file)
  113. {
  114. return ByteHashToString(this->ByteHashFile(file));
  115. }
  116. cmCryptoHashMD5::cmCryptoHashMD5()
  117. : MD5(cmsysMD5_New())
  118. {
  119. }
  120. cmCryptoHashMD5::~cmCryptoHashMD5()
  121. {
  122. cmsysMD5_Delete(this->MD5);
  123. }
  124. void cmCryptoHashMD5::Initialize()
  125. {
  126. cmsysMD5_Initialize(this->MD5);
  127. }
  128. void cmCryptoHashMD5::Append(unsigned char const* buf, int sz)
  129. {
  130. cmsysMD5_Append(this->MD5, buf, sz);
  131. }
  132. std::vector<unsigned char> cmCryptoHashMD5::Finalize()
  133. {
  134. std::vector<unsigned char> hash(16, 0);
  135. cmsysMD5_Finalize(this->MD5, &hash[0]);
  136. return hash;
  137. }
  138. #define cmCryptoHash_SHA_CLASS_IMPL(SHA) \
  139. cmCryptoHash##SHA::cmCryptoHash##SHA() \
  140. : SHA(new SHA_CTX) \
  141. { \
  142. } \
  143. cmCryptoHash##SHA::~cmCryptoHash##SHA() { delete this->SHA; } \
  144. void cmCryptoHash##SHA::Initialize() { SHA##_Init(this->SHA); } \
  145. void cmCryptoHash##SHA::Append(unsigned char const* buf, int sz) \
  146. { \
  147. SHA##_Update(this->SHA, buf, sz); \
  148. } \
  149. std::vector<unsigned char> cmCryptoHash##SHA::Finalize() \
  150. { \
  151. std::vector<unsigned char> hash(SHA##_DIGEST_LENGTH, 0); \
  152. SHA##_Final(&hash[0], this->SHA); \
  153. return hash; \
  154. }
  155. cmCryptoHash_SHA_CLASS_IMPL(SHA1) cmCryptoHash_SHA_CLASS_IMPL(SHA224)
  156. cmCryptoHash_SHA_CLASS_IMPL(SHA256) cmCryptoHash_SHA_CLASS_IMPL(SHA384)
  157. cmCryptoHash_SHA_CLASS_IMPL(SHA512)