cmCryptoHash.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmCryptoHash_h
  4. #define cmCryptoHash_h
  5. #include <cmConfigure.h>
  6. #include <cm_auto_ptr.hxx>
  7. #include <string>
  8. #include <vector>
  9. /**
  10. * @brief Abstract base class for cryptographic hash generators
  11. */
  12. class cmCryptoHash
  13. {
  14. public:
  15. virtual ~cmCryptoHash() {}
  16. /// @brief Returns a new hash generator of the requested type
  17. /// @arg algo Hash type name. Supported hash types are
  18. /// MD5, SHA1, SHA224, SHA256, SHA384, SHA512
  19. /// @return A valid auto pointer if algo is supported or
  20. /// an invalid/NULL pointer otherwise
  21. static CM_AUTO_PTR<cmCryptoHash> New(const char* algo);
  22. /// @brief Converts a hex character to its binary value (4 bits)
  23. /// @arg input Hex character [0-9a-fA-F].
  24. /// @arg output Binary value of the input character (4 bits)
  25. /// @return True if input was a valid hex character
  26. static bool IntFromHexDigit(char input, char& output);
  27. /// @brief Converts a byte hash to a sequence of hex character pairs
  28. static std::string ByteHashToString(const std::vector<unsigned char>& hash);
  29. /// @brief Calculates a binary hash from string input data
  30. /// @return Binary hash vector
  31. std::vector<unsigned char> ByteHashString(const std::string& input);
  32. /// @brief Calculates a binary hash from file content
  33. /// @see ByteHashString()
  34. /// @return Non empty binary hash vector if the file was read successfully.
  35. /// An empty vector otherwise.
  36. std::vector<unsigned char> ByteHashFile(const std::string& file);
  37. /// @brief Calculates a hash string from string input data
  38. /// @return Sequence of hex characters pairs for each byte of the binary hash
  39. std::string HashString(const std::string& input);
  40. /// @brief Calculates a hash string from file content
  41. /// @see HashString()
  42. /// @return Non empty hash string if the file was read successfully.
  43. /// An empty string otherwise.
  44. std::string HashFile(const std::string& file);
  45. protected:
  46. virtual void Initialize() = 0;
  47. virtual void Append(unsigned char const*, int) = 0;
  48. virtual std::vector<unsigned char> Finalize() = 0;
  49. };
  50. class cmCryptoHashMD5 : public cmCryptoHash
  51. {
  52. struct cmsysMD5_s* MD5;
  53. public:
  54. cmCryptoHashMD5();
  55. ~cmCryptoHashMD5() CM_OVERRIDE;
  56. protected:
  57. void Initialize() CM_OVERRIDE;
  58. void Append(unsigned char const* buf, int sz) CM_OVERRIDE;
  59. std::vector<unsigned char> Finalize() CM_OVERRIDE;
  60. };
  61. #define cmCryptoHash_SHA_CLASS_DECL(SHA) \
  62. class cmCryptoHash##SHA : public cmCryptoHash \
  63. { \
  64. union _SHA_CTX* SHA; \
  65. \
  66. public: \
  67. cmCryptoHash##SHA(); \
  68. ~cmCryptoHash##SHA(); \
  69. \
  70. protected: \
  71. virtual void Initialize(); \
  72. virtual void Append(unsigned char const* buf, int sz); \
  73. virtual std::vector<unsigned char> Finalize(); \
  74. }
  75. cmCryptoHash_SHA_CLASS_DECL(SHA1);
  76. cmCryptoHash_SHA_CLASS_DECL(SHA224);
  77. cmCryptoHash_SHA_CLASS_DECL(SHA256);
  78. cmCryptoHash_SHA_CLASS_DECL(SHA384);
  79. cmCryptoHash_SHA_CLASS_DECL(SHA512);
  80. #undef cmCryptoHash_SHA_CLASS_DECL
  81. #endif