Reputation: 539
For instance, I do an MD5 of "hashable" using:
protected string hexHashMD5(byte[] filePart) {
// Now that we have a byte array we can ask the CSP to hash it
MD5 md5 = new MD5CryptoServiceProvider();
byte[] result = md5.ComputeHash(filePart);
// Build the final string by converting each byte
// into hex and appending it to a StringBuilder
StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++) {
sb.Append(result[i].ToString("X2"));
}
// And return it
return sb.ToString();
}
and store it's output into a string variable in my program. How can I take that Hash and add another byte[] to create a new hash?
I've been told that you can use file stream, to automatically stream in a bit at a time, and make the full hash, but what happens when I need to hash two separate files together?
Upvotes: 0
Views: 394
Reputation: 887469
You can make a custom Stream
class that reads the two streams in order, then pass that to ComputeHash
.
Alternatively, you can read both streams one block at a time and pass each block to TransformBlock
:
byte[] buffer = new byte[4096];
while (true) {
int read = stream1.Read(buffer, 0, buffer.Length);
if (read == 0) break;
hash.TransformBlock(buffer, 0, read, null, 0);
}
while (true) {
int read = stream2.Read(buffer, 0, buffer.Length);
if (read == 0) break;
hash.TransformBlock(buffer, 0, read, null, 0);
}
hash.TransformFinalBlock(new byte[0], 0, 0);
var hashCode = hash.Hash;
Upvotes: 1