driis
driis

Reputation: 164331

Calculate hash without having the entire buffer in memory at once

I am doing an operation where I receive some bytes from a component, do some processing, and then send it on to the next component. I need to be able to calculate the hash of all the data I have seen at any given time - and because of data size; I cannot keep it all in a local buffer.

How would you calculate the (MD5) hash under these circumstances ?

I am thinking that I should be able to hold on to an intermediate result of the hash, and add more data as I go. But does any of the built-in framework classes support this ?

Upvotes: 6

Views: 1055

Answers (2)

Noldorin
Noldorin

Reputation: 147401

You simply want to use the TransformBlock and TransformFinalBlock members of the class, which allow you to compute the hash in chunks.

MSDN has a good example of how to do this.

Upvotes: 7

Will
Will

Reputation: 75665

Its a bit surprising that it doesn't come in the box.

If you create the MD5CryptoServiceProvider in a member variable, and call ComputeHash() repeatedly, does it not work as an append?

Upvotes: 0

Related Questions