cmCryptoHash.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file LICENSE.rst or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <cstddef>
  6. #include <iostream>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include <cm/string_view>
  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(std::vector<unsigned char> const& 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 stream content
  52. /// @see ByteHashString()
  53. /// @return Non empty binary hash vector if the stream was read successfully.
  54. /// An empty vector otherwise.
  55. std::vector<unsigned char> ByteHashStream(std::istream& sin);
  56. /// @brief Calculates a binary hash from file content
  57. /// @see ByteHashString()
  58. /// @return Non empty binary hash vector if the file was read successfully.
  59. /// An empty vector otherwise.
  60. std::vector<unsigned char> ByteHashFile(std::string const& file);
  61. /// @brief Calculates a hash string from string input data
  62. /// @return Sequence of hex characters pairs for each byte of the binary hash
  63. std::string HashString(cm::string_view input);
  64. /// @brief Calculates a hash string from stream content
  65. /// @see HashString()
  66. /// @return Non empty hash string if the stream was read successfully.
  67. /// An empty string otherwise.
  68. std::string HashStream(std::istream& sin);
  69. /// @brief Calculates a hash string from file content
  70. /// @see HashString()
  71. /// @return Non empty hash string if the file was read successfully.
  72. /// An empty string otherwise.
  73. std::string HashFile(std::string const& file);
  74. /// @brief Returns the name of the hash type.
  75. /// @return The name of the hash type associated with this hash generator.
  76. std::string GetHashAlgoName() const;
  77. void Initialize();
  78. void Append(void const*, size_t);
  79. void Append(cm::string_view input);
  80. std::vector<unsigned char> Finalize();
  81. std::string FinalizeHex();
  82. private:
  83. unsigned int Id;
  84. struct rhash_context* CTX;
  85. };