changeset 1833:47d032c48818

"OrthancPluginComputeMd5()" and "OrthancPluginComputeSha1()" to compute MD5/SHA-1 hash
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Nov 2015 12:22:44 +0100
parents b7da58699f92
children b1a6f49b21dd
files Core/Toolbox.cpp Core/Toolbox.h NEWS Plugins/Engine/OrthancPlugins.cpp Plugins/Engine/OrthancPlugins.h Plugins/Include/orthanc/OrthancCPlugin.h
diffstat 6 files changed, 148 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Toolbox.cpp	Thu Nov 26 19:09:15 2015 +0100
+++ b/Core/Toolbox.cpp	Fri Nov 27 12:22:44 2015 +0100
@@ -494,16 +494,16 @@
 
   void Toolbox::ComputeMD5(std::string& result,
                            const void* data,
-                           size_t length)
+                           size_t size)
   {
     md5_state_s state;
     md5_init(&state);
 
-    if (length > 0)
+    if (size > 0)
     {
       md5_append(&state, 
                  reinterpret_cast<const md5_byte_t*>(data), 
-                 static_cast<int>(length));
+                 static_cast<int>(size));
     }
 
     md5_byte_t actualHash[16];
@@ -759,14 +759,16 @@
     return result;
   }
 
+
   void Toolbox::ComputeSHA1(std::string& result,
-                            const std::string& data)
+                            const void* data,
+                            size_t size)
   {
     boost::uuids::detail::sha1 sha1;
 
-    if (data.size() > 0)
+    if (size > 0)
     {
-      sha1.process_bytes(&data[0], data.size());
+      sha1.process_bytes(data, size);
     }
 
     unsigned int digest[5];
@@ -785,6 +787,20 @@
             digest[4]);
   }
 
+  void Toolbox::ComputeSHA1(std::string& result,
+                            const std::string& data)
+  {
+    if (data.size() > 0)
+    {
+      ComputeSHA1(result, data.c_str(), data.size());
+    }
+    else
+    {
+      ComputeSHA1(result, NULL, 0);
+    }
+  }
+
+
   bool Toolbox::IsSHA1(const char* str,
                        size_t size)
   {
--- a/Core/Toolbox.h	Thu Nov 26 19:09:15 2015 +0100
+++ b/Core/Toolbox.h	Fri Nov 27 12:22:44 2015 +0100
@@ -100,12 +100,16 @@
 
     void ComputeMD5(std::string& result,
                     const void* data,
-                    size_t length);
+                    size_t size);
 #endif
 
     void ComputeSHA1(std::string& result,
                      const std::string& data);
 
+    void ComputeSHA1(std::string& result,
+                     const void* data,
+                     size_t size);
+
     bool IsSHA1(const char* str,
                 size_t size);
 
--- a/NEWS	Thu Nov 26 19:09:15 2015 +0100
+++ b/NEWS	Fri Nov 27 12:22:44 2015 +0100
@@ -47,6 +47,7 @@
   - "OrthancPluginGetInstanceOrigin()" to know through which mechanism an instance was received
   - "OrthancPluginCreateImage()" and "OrthancPluginCreateImageAccessor()" to create images
   - "OrthancPluginDecodeDicomImage()" to decode DICOM images
+  - "OrthancPluginComputeMd5()" and "OrthancPluginComputeSha1()" to compute MD5/SHA-1 hash
 * New events in change callbacks:
   - "OrthancStarted"
   - "OrthancStopped"
--- a/Plugins/Engine/OrthancPlugins.cpp	Thu Nov 26 19:09:15 2015 +0100
+++ b/Plugins/Engine/OrthancPlugins.cpp	Fri Nov 27 12:22:44 2015 +0100
@@ -1495,6 +1495,31 @@
   }
 
 
+  void OrthancPlugins::ComputeHash(_OrthancPluginService service,
+                                   const void* parameters)
+  {
+    const _OrthancPluginComputeHash& p =
+      *reinterpret_cast<const _OrthancPluginComputeHash*>(parameters);
+ 
+    std::string hash;
+    switch (service)
+    {
+      case _OrthancPluginService_ComputeMd5:
+        Toolbox::ComputeMD5(hash, p.buffer, p.size);
+        break;
+
+      case _OrthancPluginService_ComputeSha1:
+        Toolbox::ComputeSHA1(hash, p.buffer, p.size);
+        break;
+
+      default:
+        throw OrthancException(ErrorCode_ParameterOutOfRange);
+    }
+   
+    *p.result = CopyString(hash);
+  }
+
+
   void OrthancPlugins::ApplyCreateImage(_OrthancPluginService service,
                                         const void* parameters)
   {
@@ -2100,6 +2125,11 @@
         ApplyCreateImage(service, parameters);
         return true;
 
+      case _OrthancPluginService_ComputeMd5:
+      case _OrthancPluginService_ComputeSha1:
+        ComputeHash(service, parameters);
+        return true;
+
       default:
       {
         // This service is unknown to the Orthanc plugin engine
--- a/Plugins/Engine/OrthancPlugins.h	Thu Nov 26 19:09:15 2015 +0100
+++ b/Plugins/Engine/OrthancPlugins.h	Fri Nov 27 12:22:44 2015 +0100
@@ -150,6 +150,9 @@
     void ApplyCreateImage(_OrthancPluginService service,
                           const void* parameters);
 
+    void ComputeHash(_OrthancPluginService service,
+                     const void* parameters);
+
     void SignalChangeInternal(OrthancPluginChangeType changeType,
                               OrthancPluginResourceType resourceType,
                               const char* resource);
--- a/Plugins/Include/orthanc/OrthancCPlugin.h	Thu Nov 26 19:09:15 2015 +0100
+++ b/Plugins/Include/orthanc/OrthancCPlugin.h	Fri Nov 27 12:22:44 2015 +0100
@@ -398,6 +398,8 @@
     _OrthancPluginService_DicomBufferToJson = 21,
     _OrthancPluginService_DicomInstanceToJson = 22,
     _OrthancPluginService_CreateDicom = 23,
+    _OrthancPluginService_ComputeMd5 = 24,
+    _OrthancPluginService_ComputeSha1 = 25,
 
     /* Registration of callbacks */
     _OrthancPluginService_RegisterRestCallback = 1000,
@@ -4141,16 +4143,17 @@
    *
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param callback The callback.
+   * @return 0 if success, other value if error.
    * @ingroup Worklists
    **/
-  ORTHANC_PLUGIN_INLINE void OrthancPluginRegisterWorklistCallback(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginRegisterWorklistCallback(
     OrthancPluginContext*          context,
     OrthancPluginWorklistCallback  callback)
   {
     _OrthancPluginWorklistCallback params;
     params.callback = callback;
 
-    context->InvokeService(context, _OrthancPluginService_RegisterWorklistCallback, &params);
+    return context->InvokeService(context, _OrthancPluginService_RegisterWorklistCallback, &params);
   }
 
 
@@ -4390,16 +4393,17 @@
    *
    * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
    * @param callback The callback.
+   * @return 0 if success, other value if error.
    * @ingroup Callbacks
    **/
-  ORTHANC_PLUGIN_INLINE void OrthancPluginRegisterDecodeImageCallback(
+  ORTHANC_PLUGIN_INLINE OrthancPluginErrorCode OrthancPluginRegisterDecodeImageCallback(
     OrthancPluginContext*             context,
     OrthancPluginDecodeImageCallback  callback)
   {
     _OrthancPluginDecodeImageCallback params;
     params.callback = callback;
 
-    context->InvokeService(context, _OrthancPluginService_RegisterDecodeImageCallback, &params);
+    return context->InvokeService(context, _OrthancPluginService_RegisterDecodeImageCallback, &params);
   }
   
 
@@ -4544,6 +4548,85 @@
   }
 
 
+
+  typedef struct
+  {
+    char**       result;
+    const void*  buffer;
+    uint32_t     size;
+  } _OrthancPluginComputeHash;
+
+  /**
+   * @brief Compute an MD5 hash.
+   *
+   * This functions computes the MD5 cryptographic hash of the given memory buffer.
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param buffer The source memory buffer.
+   * @param size The size in bytes of the source buffer.
+   * @return The NULL value in case of error, or a string containing the cryptographic hash.
+   * This string must be freed by OrthancPluginFreeString().
+   * @ingroup Toolbox
+   **/
+  ORTHANC_PLUGIN_INLINE char* OrthancPluginComputeMd5(
+    OrthancPluginContext*  context,
+    const void*            buffer,
+    uint32_t               size)
+  {
+    char* result;
+
+    _OrthancPluginComputeHash params;
+    params.result = &result;
+    params.buffer = buffer;
+    params.size = size;
+
+    if (context->InvokeService(context, _OrthancPluginService_ComputeMd5, &params) != OrthancPluginErrorCode_Success)
+    {
+      /* Error */
+      return NULL;
+    }
+    else
+    {
+      return result;
+    }
+  }
+
+
+  /**
+   * @brief Compute a SHA-1 hash.
+   *
+   * This functions computes the SHA-1 cryptographic hash of the given memory buffer.
+   * 
+   * @param context The Orthanc plugin context, as received by OrthancPluginInitialize().
+   * @param buffer The source memory buffer.
+   * @param size The size in bytes of the source buffer.
+   * @return The NULL value in case of error, or a string containing the cryptographic hash.
+   * This string must be freed by OrthancPluginFreeString().
+   * @ingroup Toolbox
+   **/
+  ORTHANC_PLUGIN_INLINE char* OrthancPluginComputeSha1(
+    OrthancPluginContext*  context,
+    const void*            buffer,
+    uint32_t               size)
+  {
+    char* result;
+
+    _OrthancPluginComputeHash params;
+    params.result = &result;
+    params.buffer = buffer;
+    params.size = size;
+
+    if (context->InvokeService(context, _OrthancPluginService_ComputeSha1, &params) != OrthancPluginErrorCode_Success)
+    {
+      /* Error */
+      return NULL;
+    }
+    else
+    {
+      return result;
+    }
+  }
+
 #ifdef  __cplusplus
 }
 #endif