Incremental Hashing in WinRT/Metro

﹥>﹥吖頭↗ 提交于 2019-12-11 01:27:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!