Browse Source

cmCryptoHash: Add function which gets the hash type name

Deniz Bahadir 1 year ago
parent
commit
5fa5bde5d6
2 changed files with 37 additions and 0 deletions
  1. 33 0
      Source/cmCryptoHash.cxx
  2. 4 0
      Source/cmCryptoHash.h

+ 33 - 0
Source/cmCryptoHash.cxx

@@ -2,6 +2,8 @@
    file Copyright.txt or https://cmake.org/licensing for details.  */
 #include "cmCryptoHash.h"
 
+#include <cassert>
+
 #include <cm/memory>
 
 #include <cm3p/kwiml/int.h>
@@ -80,6 +82,37 @@ std::unique_ptr<cmCryptoHash> cmCryptoHash::New(cm::string_view algo)
   return std::unique_ptr<cmCryptoHash>(nullptr);
 }
 
+std::string cmCryptoHash::GetHashAlgoName() const
+{
+#ifndef CMAKE_USE_SYSTEM_LIBRHASH
+  static_assert(RHASH_HASH_COUNT == 10, "Update switch statement!");
+#endif
+  switch (this->Id) {
+    case RHASH_MD5:
+      return "MD5";
+    case RHASH_SHA1:
+      return "SHA1";
+    case RHASH_SHA224:
+      return "SHA224";
+    case RHASH_SHA256:
+      return "SHA256";
+    case RHASH_SHA384:
+      return "SHA384";
+    case RHASH_SHA512:
+      return "SHA512";
+    case RHASH_SHA3_224:
+      return "SHA3_224";
+    case RHASH_SHA3_256:
+      return "SHA3_256";
+    case RHASH_SHA3_384:
+      return "SHA3_384";
+    case RHASH_SHA3_512:
+      return "SHA3_512";
+  }
+  assert(false);
+  return "UNKNOWN";
+}
+
 bool cmCryptoHash::IntFromHexDigit(char input, char& output)
 {
   if (input >= '0' && input <= '9') {

+ 4 - 0
Source/cmCryptoHash.h

@@ -74,6 +74,10 @@ public:
   ///         An empty string otherwise.
   std::string HashFile(const std::string& file);
 
+  /// @brief Returns the name of the hash type.
+  /// @return The name of the hash type associated with this hash generator.
+  std::string GetHashAlgoName() const;
+
   void Initialize();
   void Append(void const*, size_t);
   void Append(cm::string_view input);