CMake Nightly Date Stamp
[kiteware-cmake.git] / Source / cmCryptoHash.h
blobbb026a2fb635c14169b10b23761b392fb61d7665
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
5 #include "cmConfigure.h" // IWYU pragma: keep
7 #include <cstddef>
8 #include <memory>
9 #include <string>
10 #include <vector>
12 #include <cm/string_view>
14 /**
15 * @brief Abstract base class for cryptographic hash generators
17 class cmCryptoHash
19 public:
20 enum Algo
22 AlgoMD5,
23 AlgoSHA1,
24 AlgoSHA224,
25 AlgoSHA256,
26 AlgoSHA384,
27 AlgoSHA512,
28 AlgoSHA3_224,
29 AlgoSHA3_256,
30 AlgoSHA3_384,
31 AlgoSHA3_512
34 cmCryptoHash(Algo algo);
35 ~cmCryptoHash();
37 cmCryptoHash(cmCryptoHash const&) = delete;
38 cmCryptoHash& operator=(cmCryptoHash const&) = delete;
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);
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);
54 /// @brief Converts a byte hash to a sequence of hex character pairs
55 static std::string ByteHashToString(const std::vector<unsigned char>& hash);
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);
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);
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);
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);
77 /// @brief Returns the name of the hash type.
78 /// @return The name of the hash type associated with this hash generator.
79 std::string GetHashAlgoName() const;
81 void Initialize();
82 void Append(void const*, size_t);
83 void Append(cm::string_view input);
84 std::vector<unsigned char> Finalize();
85 std::string FinalizeHex();
87 private:
88 unsigned int Id;
89 struct rhash_context* CTX;