diff Core/Toolbox.cpp @ 22:1bc6327d1de3

MD5 sum
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 27 Aug 2012 11:45:53 +0200
parents 27e2bbc11200
children 62bd05fe4b7c
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Tue Jul 31 17:56:27 2012 +0200
+++ b/Core/Toolbox.cpp	Mon Aug 27 11:45:53 2012 +0200
@@ -36,24 +36,26 @@
 #include <unistd.h>
 #endif
 
+#include "../Resources/md5/md5.h"
+
 
 namespace Palantir
 {
   static bool finish;
 
 #if defined(_WIN32)
-  static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
-  {
-	// http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx
-	finish = true;
-	return true;
-  }
-#else
+  static BOOL WINAPI ConsoleControlHandler(DWORD dwCtrlType)
+  {
+	// http://msdn.microsoft.com/en-us/library/ms683242(v=vs.85).aspx
+	finish = true;
+	return true;
+  }
+#else
   static void SignalHandler(int)
   {
     finish = true;
   }
-#endif
+#endif
 
   void Toolbox::Sleep(uint32_t seconds)
   {
@@ -302,4 +304,40 @@
       throw PalantirException(ErrorCode_InexistentFile);
     }
   }
+
+
+
+  static char GetHexadecimalCharacter(uint8_t value)
+  {
+    assert(value < 16);
+
+    if (value < 10)
+      return value + '0';
+    else
+      return (value - 10) + 'a';
+  }
+
+  void Toolbox::ComputeMD5(std::string& result,
+                           const std::string& data)
+  {
+    md5_state_s state;
+    md5_init(&state);
+
+    if (data.size() > 0)
+    {
+      md5_append(&state, reinterpret_cast<const md5_byte_t*>(&data[0]), 
+                 static_cast<int>(data.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(actualHash[i] / 16);
+      result[2 * i + 1] = GetHexadecimalCharacter(actualHash[i] % 16);
+    }
+
+  }
 }