在 C# 中获取字节数组内容的 MD5 校验和
Get MD5 checksum of Byte Arrays' conent in C#
我在 Python 中编写了一个脚本,它为我提供了字节数组内容的 MD5 校验和。
strz = xor(dataByteArray, key)
m = hashlib.md5()
m.update(strz)
然后我可以像这样比较硬编码的 MD5 和 m:
if m.hexdigest() == hardCodedHash:
有没有办法用 C# 做同样的事情?到目前为止我找到的唯一资源还不够清楚。
这是计算 MD5 散列的方法
byte[] hash;
using (var md5 = System.Security.Cryptography.MD5.Create()) {
md5.TransformFinalBlock(dataByteArray, 0, dataByteArray.Length);
hash = md5.Hash;
}
然后您将该散列(逐字节)与您已知的散列进行比较
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();
}
}
我在 Python 中编写了一个脚本,它为我提供了字节数组内容的 MD5 校验和。
strz = xor(dataByteArray, key)
m = hashlib.md5()
m.update(strz)
然后我可以像这样比较硬编码的 MD5 和 m:
if m.hexdigest() == hardCodedHash:
有没有办法用 C# 做同样的事情?到目前为止我找到的唯一资源还不够清楚。
这是计算 MD5 散列的方法
byte[] hash;
using (var md5 = System.Security.Cryptography.MD5.Create()) {
md5.TransformFinalBlock(dataByteArray, 0, dataByteArray.Length);
hash = md5.Hash;
}
然后您将该散列(逐字节)与您已知的散列进行比较
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();
}
}