當然。您可以重複調用TransformBlock
,然後在最後使用TransformFinalBlock
,然後使用Hash
獲取最終的哈希值。因此,像:
using (var md5 = MD5.Create()) // Or MD5Cng.Create
using (var sha1 = SHA1.Create()) // Or SHA1Cng.Create
using (var input = File.OpenRead("file.data"))
{
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length()) > 0)
{
md5.TransformBlock(buffer, 0, bytesRead, buffer, 0);
sha1.TransformBlock(buffer, 0, bytesRead, buffer, 0);
}
// We have to call TransformFinalBlock, but we don't have any
// more data - just provide 0 bytes.
md5.TransformFinalBlock(buffer, 0, 0, buffer, 0);
sha1.TransformFinalBlock(buffer, 0, 0, buffer, 0);
byte[] md5Hash = md5.Hash;
byte[] sha1Hash = sha1.Hash;
}
的MD5Cng.Create
和SHA1Cng.Create
通話將創建一個圍繞本地實現這可能是比MD5.Create
和SHA1.Create
返回實現更快的包裝,但是這將是便攜式的(例如用於PCLS)有點少。
將其逐段讀取並將數據提供給您自己的摘要算法串聯 – sehe 2013-02-15 22:41:42
檢查:http://stackoverflow.com/questions/14610850/how-to-get-file-both-md5-and-sha1-checksum- at-the-same-time-when-upload-a-new-fi(java) – PunKeel 2013-02-15 22:42:33
http://stackoverflow.com/questions/7832440/is-hashalgorithm-computehash-stateful – 2013-02-15 22:43:18