cmCryptoHash.cxx 5.8 KB

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