# HG changeset patch # User Alain Mazy # Date 1747855758 -7200 # Node ID e919f8d24f092603867b247ad6894622a48056c2 # Parent 223167d90dbe2c08940fba20aa8c57a5b1bf5935 Files MD5 diff -r 223167d90dbe -r e919f8d24f09 OrthancFramework/Sources/SystemToolbox.cpp --- 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) { diff -r 223167d90dbe -r e919f8d24f09 OrthancFramework/Sources/SystemToolbox.h --- 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); diff -r 223167d90dbe -r e919f8d24f09 OrthancFramework/Sources/Toolbox.h --- 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& data); + + static void ComputeMD5(std::string& result, + std::istream& stream); #endif static void ComputeSHA1(std::string& result, diff -r 223167d90dbe -r e919f8d24f09 OrthancFramework/UnitTestsSources/FrameworkTests.cpp --- 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) {