changeset 6129:e919f8d24f09 attach-custom-data

Files MD5
author Alain Mazy <am@orthanc.team>
date Wed, 21 May 2025 21:29:18 +0200
parents 223167d90dbe
children 31d41a52ac2f
files OrthancFramework/Sources/SystemToolbox.cpp OrthancFramework/Sources/SystemToolbox.h OrthancFramework/Sources/Toolbox.h OrthancFramework/UnitTestsSources/FrameworkTests.cpp
diffstat 4 files changed, 85 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancFramework/Sources/SystemToolbox.cpp	Tue May 20 19:17:13 2025 +0200
+++ b/OrthancFramework/Sources/SystemToolbox.cpp	Wed May 21 21:29:18 2025 +0200
@@ -454,6 +454,31 @@
     }
   }
 
+#if ORTHANC_ENABLE_MD5 == 1
+  void SystemToolbox::ComputeFileMD5(std::string& result,
+                                     const std::string& path)
+  {
+    std::ifstream fileStream(path, std::ifstream::binary);
+    Toolbox::ComputeMD5(result, fileStream);
+  }
+
+  bool SystemToolbox::CompareFilesMD5(const std::string& path1,
+                                      const std::string& path2)
+  {
+    if (SystemToolbox::GetFileSize(path1) != SystemToolbox::GetFileSize(path2))
+    {
+      return false;
+    }
+
+    std::string path1md5, path2md5;
+    
+    SystemToolbox::ComputeFileMD5(path1md5, path1);
+    SystemToolbox::ComputeFileMD5(path2md5, path2);
+
+    return path1md5 == path2md5;
+  }
+#endif
+
 
   void SystemToolbox::MakeDirectory(const std::string& path)
   {
--- a/OrthancFramework/Sources/SystemToolbox.h	Tue May 20 19:17:13 2025 +0200
+++ b/OrthancFramework/Sources/SystemToolbox.h	Wed May 21 21:29:18 2025 +0200
@@ -83,6 +83,15 @@
 
     static uint64_t GetFileSize(const std::string& path);
 
+#if ORTHANC_ENABLE_MD5 == 1
+    static void ComputeFileMD5(std::string& result,
+                               const std::string& path);
+
+    // returns true if file have the same MD5
+    static bool CompareFilesMD5(const std::string& path1,
+                                const std::string& path2); 
+#endif
+
     static void MakeDirectory(const std::string& path);
 
     static bool IsExistingFile(const std::string& path);
--- a/OrthancFramework/Sources/Toolbox.h	Tue May 20 19:17:13 2025 +0200
+++ b/OrthancFramework/Sources/Toolbox.h	Wed May 21 21:29:18 2025 +0200
@@ -134,6 +134,9 @@
 
     static void ComputeMD5(std::string& result,
                            const std::set<std::string>& data);
+
+    static void ComputeMD5(std::string& result,
+                           std::istream& stream);
 #endif
 
     static void ComputeSHA1(std::string& result,
--- a/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Tue May 20 19:17:13 2025 +0200
+++ b/OrthancFramework/UnitTestsSources/FrameworkTests.cpp	Wed May 21 21:29:18 2025 +0200
@@ -397,6 +397,10 @@
 
   Toolbox::ComputeMD5(s, set);
   ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", s); // set md5 same as string with the values sorted
+
+  std::istringstream iss(std::string("aaabbbccc"));
+  Toolbox::ComputeMD5(s, iss);
+  ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", s);
 }
 
 TEST(Toolbox, ComputeSHA1)
@@ -1591,6 +1595,50 @@
 #endif
 
 
+#if ORTHANC_SANDBOXED != 1 && ORTHANC_ENABLE_MD5 == 1
+TEST(Toolbox, FileMD5)
+{
+  std::string path;
+
+  {
+    TemporaryFile tmp1, tmp2;
+    std::string s = "aaabbbccc";
+
+    SystemToolbox::WriteFile(s, tmp1.GetPath());
+    SystemToolbox::WriteFile(s, tmp2.GetPath());
+
+    std::string md5;
+    SystemToolbox::ComputeFileMD5(md5, tmp1.GetPath());
+
+    ASSERT_EQ("d1aaf4767a3c10a473407a4e47b02da6", md5);
+    ASSERT_TRUE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath()));
+  }
+
+  { // different sizes
+    TemporaryFile tmp1, tmp2;
+    std::string s1 = "aaabbbccc";
+    std::string s2 = "aaabbbcccd";
+
+    SystemToolbox::WriteFile(s1, tmp1.GetPath());
+    SystemToolbox::WriteFile(s2, tmp2.GetPath());
+
+    ASSERT_FALSE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath()));
+  }
+
+  { // same sizes, different contents
+    TemporaryFile tmp1, tmp2;
+    std::string s1 = "aaabbbccc";
+    std::string s2 = "aaabbbccd";
+
+    SystemToolbox::WriteFile(s1, tmp1.GetPath());
+    SystemToolbox::WriteFile(s2, tmp2.GetPath());
+
+    ASSERT_FALSE(SystemToolbox::CompareFilesMD5(tmp1.GetPath(), tmp2.GetPath()));
+  }
+
+}
+#endif
+
 #if ORTHANC_SANDBOXED != 1
 TEST(Toolbox, GetMacAddressess)
 {