cmCryptoHash.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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> // IWYU pragma: keep
  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. CM_DISABLE_COPY(cmCryptoHash)
  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. /// @brief Returns a new hash generator of the requested type
  33. /// @arg algo Hash type name. Supported hash types are
  34. /// MD5, SHA1, SHA224, SHA256, SHA384, SHA512,
  35. /// SHA3_224, SHA3_256, SHA3_384, SHA3_512
  36. /// @return A valid auto pointer if algo is supported or
  37. /// an invalid/NULL pointer otherwise
  38. static std::unique_ptr<cmCryptoHash> New(const char* algo);
  39. /// @brief Converts a hex character to its binary value (4 bits)
  40. /// @arg input Hex character [0-9a-fA-F].
  41. /// @arg output Binary value of the input character (4 bits)
  42. /// @return True if input was a valid hex character
  43. static bool IntFromHexDigit(char input, char& output);
  44. /// @brief Converts a byte hash to a sequence of hex character pairs
  45. static std::string ByteHashToString(const std::vector<unsigned char>& hash);
  46. /// @brief Calculates a binary hash from string input data
  47. /// @return Binary hash vector
  48. std::vector<unsigned char> ByteHashString(const std::string& input);
  49. /// @brief Calculates a binary hash from file content
  50. /// @see ByteHashString()
  51. /// @return Non empty binary hash vector if the file was read successfully.
  52. /// An empty vector otherwise.
  53. std::vector<unsigned char> ByteHashFile(const std::string& file);
  54. /// @brief Calculates a hash string from string input data
  55. /// @return Sequence of hex characters pairs for each byte of the binary hash
  56. std::string HashString(const std::string& input);
  57. /// @brief Calculates a hash string from file content
  58. /// @see HashString()
  59. /// @return Non empty hash string if the file was read successfully.
  60. /// An empty string otherwise.
  61. std::string HashFile(const std::string& file);
  62. void Initialize();
  63. void Append(void const*, size_t);
  64. void Append(std::string const& str);
  65. std::vector<unsigned char> Finalize();
  66. std::string FinalizeHex();
  67. private:
  68. unsigned int Id;
  69. struct rhash_context* CTX;
  70. };
  71. #endif