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