changeset 5377:daf4807631c5

ComputeMD5 for std::set
author Alain Mazy <am@osimis.io>
date Thu, 24 Aug 2023 13:35:18 +0200
parents 4ab905749aed
children d857c6210c50
files OrthancFramework/Sources/Toolbox.cpp OrthancFramework/Sources/Toolbox.h OrthancFramework/UnitTestsSources/FrameworkTests.cpp
diffstat 3 files changed, 46 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/Toolbox.cpp	Tue Aug 22 16:35:22 2023 +0200
+++ b/OrthancFramework/Sources/Toolbox.cpp	Thu Aug 24 13:35:18 2023 +0200
@@ -496,6 +496,34 @@
       result[2 * i + 1] = GetHexadecimalCharacter(static_cast<uint8_t>(actualHash[i] % 16));
     }
   }
+
+  void Toolbox::ComputeMD5(std::string& result,
+                           const std::set<std::string>& data)
+  {
+    md5_state_s state;
+    md5_init(&state);
+
+    if (data.size() > 0)
+    {
+      for (std::set<std::string>::const_iterator it = data.begin(); it != data.end(); ++it)
+      {
+        md5_append(&state, 
+                   reinterpret_cast<const md5_byte_t*>(it->c_str()), 
+                   static_cast<int>(it->size()));
+      }
+    }
+
+    md5_byte_t actualHash[16];
+    md5_finish(&state, actualHash);
+
+    result.resize(32);
+    for (unsigned int i = 0; i < 16; i++)
+    {
+      result[2 * i] = GetHexadecimalCharacter(static_cast<uint8_t>(actualHash[i] / 16));
+      result[2 * i + 1] = GetHexadecimalCharacter(static_cast<uint8_t>(actualHash[i] % 16));
+    }
+  }
+
 #endif
 
 
--- a/OrthancFramework/Sources/Toolbox.h	Tue Aug 22 16:35:22 2023 +0200
+++ b/OrthancFramework/Sources/Toolbox.h	Thu Aug 24 13:35:18 2023 +0200
@@ -128,6 +128,9 @@
     static void ComputeMD5(std::string& result,
                            const void* data,
                            size_t size);
+
+    static void ComputeMD5(std::string& result,
+                           const std::set<std::string>& data);
 #endif
 
     static void ComputeSHA1(std::string& result,
--- a/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Tue Aug 22 16:35:22 2023 +0200
+++ b/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Thu Aug 24 13:35:18 2023 +0200
@@ -381,6 +381,21 @@
   ASSERT_EQ("8b1a9953c4611296a827abf8c47804d7", s);
   Toolbox::ComputeMD5(s, "");
   ASSERT_EQ("d41d8cd98f00b204e9800998ecf8427e", s);
+
+  Toolbox::ComputeMD5(s, "aaabbbccc");
+  ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", s);
+
+  std::set<std::string> set;
+
+  Toolbox::ComputeMD5(s, set);
+  ASSERT_EQ("d41d8cd98f00b204e9800998ecf8427e", s);  // empty set same as empty string
+
+  set.insert("bbb");
+  set.insert("ccc");
+  set.insert("aaa");
+
+  Toolbox::ComputeMD5(s, set);
+  ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", s); // set md5 same as string with the values sorted
 }
 
 TEST(Toolbox, ComputeSHA1)