cmCryptoHash.cxx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <cmsys/MD5.h>
  12. #include "cm_sha2.h"
  13. //----------------------------------------------------------------------------
  14. cmsys::auto_ptr<cmCryptoHash> cmCryptoHash::New(const char* algo)
  15. {
  16. if(strcmp(algo,"MD5") == 0)
  17. { return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashMD5); }
  18. else if(strcmp(algo,"SHA1") == 0)
  19. { return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA1); }
  20. else if(strcmp(algo,"SHA224") == 0)
  21. { return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA224); }
  22. else if(strcmp(algo,"SHA256") == 0)
  23. { return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA256); }
  24. else if(strcmp(algo,"SHA384") == 0)
  25. { return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA384); }
  26. else if(strcmp(algo,"SHA512") == 0)
  27. { return cmsys::auto_ptr<cmCryptoHash>(new cmCryptoHashSHA512); }
  28. else
  29. { return cmsys::auto_ptr<cmCryptoHash>(0); }
  30. }
  31. //----------------------------------------------------------------------------
  32. std::string cmCryptoHash::HashString(const char* input)
  33. {
  34. this->Initialize();
  35. this->Append(reinterpret_cast<unsigned char const*>(input),
  36. static_cast<int>(strlen(input)));
  37. return this->Finalize();
  38. }
  39. //----------------------------------------------------------------------------
  40. std::string cmCryptoHash::HashFile(const char* file)
  41. {
  42. std::ifstream fin(file, std::ios::in | cmsys_ios_binary);
  43. if(!fin)
  44. {
  45. return "";
  46. }
  47. this->Initialize();
  48. // Should be efficient enough on most system:
  49. const int bufferSize = 4096;
  50. char buffer[bufferSize];
  51. unsigned char const* buffer_uc =
  52. reinterpret_cast<unsigned char const*>(buffer);
  53. // This copy loop is very sensitive on certain platforms with
  54. // slightly broken stream libraries (like HPUX). Normally, it is
  55. // incorrect to not check the error condition on the fin.read()
  56. // before using the data, but the fin.gcount() will be zero if an
  57. // error occurred. Therefore, the loop should be safe everywhere.
  58. while(fin)
  59. {
  60. fin.read(buffer, bufferSize);
  61. if(int gcount = static_cast<int>(fin.gcount()))
  62. {
  63. this->Append(buffer_uc, gcount);
  64. }
  65. }
  66. if(fin.eof())
  67. {
  68. return this->Finalize();
  69. }
  70. return "";
  71. }
  72. //----------------------------------------------------------------------------
  73. cmCryptoHashMD5::cmCryptoHashMD5(): MD5(cmsysMD5_New())
  74. {
  75. }
  76. //----------------------------------------------------------------------------
  77. cmCryptoHashMD5::~cmCryptoHashMD5()
  78. {
  79. cmsysMD5_Delete(this->MD5);
  80. }
  81. //----------------------------------------------------------------------------
  82. void cmCryptoHashMD5::Initialize()
  83. {
  84. cmsysMD5_Initialize(this->MD5);
  85. }
  86. //----------------------------------------------------------------------------
  87. void cmCryptoHashMD5::Append(unsigned char const* buf, int sz)
  88. {
  89. cmsysMD5_Append(this->MD5, buf, sz);
  90. }
  91. //----------------------------------------------------------------------------
  92. std::string cmCryptoHashMD5::Finalize()
  93. {
  94. char md5out[32];
  95. cmsysMD5_FinalizeHex(this->MD5, md5out);
  96. return std::string(md5out, 32);
  97. }
  98. #define cmCryptoHash_SHA_CLASS_IMPL(SHA) \
  99. cmCryptoHash##SHA::cmCryptoHash##SHA(): SHA(new SHA_CTX) {} \
  100. cmCryptoHash##SHA::~cmCryptoHash##SHA() { delete this->SHA; } \
  101. void cmCryptoHash##SHA::Initialize() { SHA##_Init(this->SHA); } \
  102. void cmCryptoHash##SHA::Append(unsigned char const* buf, int sz) \
  103. { SHA##_Update(this->SHA, buf, sz); } \
  104. std::string cmCryptoHash##SHA::Finalize() \
  105. { \
  106. char out[SHA##_DIGEST_STRING_LENGTH]; \
  107. SHA##_End(this->SHA, out); \
  108. return std::string(out, SHA##_DIGEST_STRING_LENGTH-1); \
  109. }
  110. cmCryptoHash_SHA_CLASS_IMPL(SHA1)
  111. cmCryptoHash_SHA_CLASS_IMPL(SHA224)
  112. cmCryptoHash_SHA_CLASS_IMPL(SHA256)
  113. cmCryptoHash_SHA_CLASS_IMPL(SHA384)
  114. cmCryptoHash_SHA_CLASS_IMPL(SHA512)