How to convert this C # code to C ++ / CLI

How to convert this segment of C # code to C ++ / CLI:

protected string GetMD5HashFromFile(string fileName)
{
  FileStream file = new FileStream(fileName, FileMode.Open);
  MD5 md5 = new MD5CryptoServiceProvider();
  byte[] retVal = md5.ComputeHash(file);
  file.Close();
  ASCIIEncoding enc = new ASCIIEncoding();
  return enc.GetString(retVal);
}

Specially this part byte[] retVal = md5.ComputeHash(file);

+2
source share
2 answers

Free use of the stack semantics available in C ++ / CLI to automatically delete objects. Emulating a Holy C ++ RAII template, an object becomes available even when the code throws an exception. Think about it as the compiler automatically generates a C # using statement. Like this:

using namespace System;
using namespace System::IO;
using namespace System::Security::Cryptography;
using namespace System::Text;

ref class Example {
protected:
    String^ GetMD5HashFromFile(String^ fileName)
    {      
        FileStream file(fileName, FileMode::Open);
        MD5CryptoServiceProvider md5;
        array<Byte>^ retVal = md5.ComputeHash(%file);
        return Convert::ToBase64String(retVal);
    }
};
+8
source

Here is an example of using a crypto service provider from C ++ to generate MD5 in the top answer to this question:

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/c0f97655-d953-4e3f-82b9-b70edaf1625b/

0

All Articles