cmCryptoHash.h 2.7 KB

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