cmCryptoHash.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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" // IWYU pragma: keep
  6. #include <memory>
  7. #include <stddef.h>
  8. #include <string>
  9. #include <vector>
  10. /**
  11. * @brief Abstract base class for cryptographic hash generators
  12. */
  13. class cmCryptoHash
  14. {
  15. public:
  16. enum Algo
  17. {
  18. AlgoMD5,
  19. AlgoSHA1,
  20. AlgoSHA224,
  21. AlgoSHA256,
  22. AlgoSHA384,
  23. AlgoSHA512,
  24. AlgoSHA3_224,
  25. AlgoSHA3_256,
  26. AlgoSHA3_384,
  27. AlgoSHA3_512
  28. };
  29. cmCryptoHash(Algo algo);
  30. ~cmCryptoHash();
  31. cmCryptoHash(cmCryptoHash const&) = delete;
  32. cmCryptoHash& operator=(cmCryptoHash const&) = delete;
  33. /// @brief Returns a new hash generator of the requested type
  34. /// @arg algo Hash type name. Supported hash types are
  35. /// MD5, SHA1, SHA224, SHA256, SHA384, SHA512,
  36. /// SHA3_224, SHA3_256, SHA3_384, SHA3_512
  37. /// @return A valid auto pointer if algo is supported or
  38. /// an invalid/NULL pointer otherwise
  39. static std::unique_ptr<cmCryptoHash> New(const char* algo);
  40. /// @brief Converts a hex character to its binary value (4 bits)
  41. /// @arg input Hex character [0-9a-fA-F].
  42. /// @arg output Binary value of the input character (4 bits)
  43. /// @return True if input was a valid hex character
  44. static bool IntFromHexDigit(char input, char& output);
  45. /// @brief Converts a byte hash to a sequence of hex character pairs
  46. static std::string ByteHashToString(const std::vector<unsigned char>& hash);
  47. /// @brief Calculates a binary hash from string input data
  48. /// @return Binary hash vector
  49. std::vector<unsigned char> ByteHashString(const std::string& input);
  50. /// @brief Calculates a binary hash from file content
  51. /// @see ByteHashString()
  52. /// @return Non empty binary hash vector if the file was read successfully.
  53. /// An empty vector otherwise.
  54. std::vector<unsigned char> ByteHashFile(const std::string& file);
  55. /// @brief Calculates a hash string from string input data
  56. /// @return Sequence of hex characters pairs for each byte of the binary hash
  57. std::string HashString(const std::string& input);
  58. /// @brief Calculates a hash string from file content
  59. /// @see HashString()
  60. /// @return Non empty hash string if the file was read successfully.
  61. /// An empty string otherwise.
  62. std::string HashFile(const std::string& file);
  63. void Initialize();
  64. void Append(void const*, size_t);
  65. void Append(std::string const& str);
  66. std::vector<unsigned char> Finalize();
  67. std::string FinalizeHex();
  68. private:
  69. unsigned int Id;
  70. struct rhash_context* CTX;
  71. };
  72. #endif