'Get MD5 checksum of Byte Arrays' conent in C#
I wrote a script in Python which gives me an MD5 checksum for a byte array's content.
strz = xor(dataByteArray, key)
m = hashlib.md5()
m.update(strz)
I can then compare a hardcoded MD5 with m like so:
if m.hexdigest() == hardCodedHash:
Is there a way to do the same thing with C#? The only resources i've found so far are not clear enough.
Solution 1:[1]
Here's how you compute the MD5 hash
byte[] hash;
using (var md5 = System.Security.Cryptography.MD5.Create()) {
md5.TransformFinalBlock(dataByteArray, 0, dataByteArray.Length);
hash = md5.Hash;
}
you would then compare that hash (byte by byte) to your known hash
Solution 2:[2]
public static string GetMD5checksum(byte[] inputData)
{
//convert byte array to stream
System.IO.MemoryStream stream = new System.IO.MemoryStream();
stream.Write(inputData, 0, inputData.Length);
//important: get back to start of stream
stream.Seek(0, System.IO.SeekOrigin.Begin);
//get a string value for the MD5 hash.
using (var md5Instance = System.Security.Cryptography.MD5.Create())
{
var hashResult = md5Instance.ComputeHash(stream);
//***I did some formatting here, you may not want to remove the dashes, or use lower case depending on your application
return BitConverter.ToString(hashResult).Replace("-", "").ToLowerInvariant();
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tim |
Solution 2 | mike |