System.Security.Policy.Hash 能够计算哈希,而不管程序集的位置如何。因此,我们至少有两种方法可以将程序集作为字节数组获取:
1)使用反射:
var hash = new Hash(assembly);
var dllAsArray = (byte[]) hash.GetType()
.GetMethod("GetRawData", BindingFlags.Instance | BindingFlags.NonPublic)
.Invoke(hash, new object[0]);
2) 使用假的 HashAlgorithm 实现:
public class GetWholeBodyPseudoHash : HashAlgorithm
{
protected override void Dispose(bool disposing)
{
if(disposing) _memoryStream.Dispose();
base.Dispose(disposing);
}
static GetWholeBodyPseudoHash()
{
CryptoConfig.AddAlgorithm(typeof(GetWholeBodyPseudoHash), typeof(GetWholeBodyPseudoHash).FullName);
}
private MemoryStream _memoryStream=new MemoryStream();
public override void Initialize()
{
_memoryStream.Dispose();
_memoryStream = new MemoryStream();
}
protected override void HashCore(byte[] array, int ibStart, int cbSize)
{
_memoryStream.Write(array, ibStart, cbSize);
}
protected override byte[] HashFinal()
{
return _memoryStream.ToArray();
}
}
...
var bytes = new Hash(yourAssembly).GenerateHash(new GetWholeBodyPseudoHash());