views:

41

answers:

2

I am trying to translate some existing C++ code to C#. I have some code which calls CryptHashData 3 times on the same HCRYPTHASH. The documentation says "This function and CryptHashSessionKey can be called multiple times to compute the hash of long or discontinuous data streams."

Which is what I want to achieve in C#. Unfortunately, MD5.ComputeHash() doesn't appear to have a way to build on the existing hash.

Am I missing something, is there a C# API that would achieve this?

...Stefan

A: 

The cryptography functions in C# are limited. It's better to import CryptoAPI in C#.

[DllImport("Advapi32.dll", SetLastError=true)]
public static extern Boolean CryptHashData (
    IntPtr hHash,
    Byte[] pbData,
    Int32 dwDataLen,
    Int32 dwFlags
);

Look at this and this for more.

lalli
In what way are the limited?
GregS
A: 

Instantiate an instance of the hash class and use TransformBlock and TransformFinalBlock:

byte[] part1 = //...
byte[] part2 = //...
byte[] part3 = //...
var hash = new MD5CryptoServiceProvider();
hash.TransformBlock(part1, 0, part1.Length, null, 0);
hash.TransformBlock(part2, 0, part2.Length, null, 0);
hash.TransformFinalBlock(part3, 0, part3.Length);
byte[] res = hash.Hash;
Rasmus Faber
Thanks very much!
Stefan