问题
I'm porting some code that uses incremental SHA-1 heavily:
SHA1 hasher = HashAlgoFactory.Create<SHA1>();
hasher.Initialize();
DiskIOCallback readCallback = null;
readCallback = delegate(bool successful) {
if (successful)
hasher.TransformBlock(hashBuffer, 0, count, hashBuffer, 0);
offset += count;
if (!successful || offset == endOffset)
{
object hash = null;
if (successful)
{
hasher.TransformFinalBlock(hashBuffer, 0, 0);
hash = hasher.Hash;
}
And am looking for a WinRT/Metro equivalent. I've used hasher.HashData before; is there some relatively simple way to get incremental behaviour out of HashData, or some alternative that implements the same functionality as above (in a WinRT/Metro way)? It's entirely possible I'm missing something obvious...
回答1:
You should use CryptographicHash class:
var hashProvider = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
var hasher = hashProvider.CreateHash();
You can append multiple increments of data:
hasher.Append(bytesPart.AsBuffer());
Once you're done, you retrieve the hash:
var hash = hasher.GetValueAndReset().ToArray();
来源:https://stackoverflow.com/questions/13982546/incremental-hashing-in-winrt-metro