我有一个散列算法对象(在本例中为 SHA1),我向其提供数据,以便在调用 Result 属性时最终获得散列结果。
问题是一旦调用了 m_HashAlgorithm.Hash 属性,该对象就不能再用于喂食。如果我尝试输入它,我会得到: System.Security.Cryptography.CryptographicUnexpectedOperationException: Hash must be finalized before the hash value is retrieved.
我需要能够获得中间散列结果,但继续喂食并稍后获得另一个结果。有没有办法实现它?
private readonly HashAlgorithm m_HashAlgorithm;
public DigitalSignatureCreator(HashAlgorithm hashAlgorithm)
{
m_HashAlgorithm = hashAlgorithm;
m_MemoryStreamEncrypt = new MemoryStream();
m_CryptoStreamEncrypt = new CryptoStream(m_MemoryStreamEncrypt, m_HashAlgorithm, CryptoStreamMode.Write);
}
public void Feed(byte[] data, int offset, int count)
{
m_CryptoStreamEncrypt.Write(data, offset, count);
}
public byte[] Result
{
get
{
return m_HashAlgorithm.Hash;
}
}