散列多个字节[]的同时与C#一个哈希? [英] Hashing multiple byte[]'s together into a single hash with C#?

查看:117
本文介绍了散列多个字节[]的同时与C#一个哈希?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个字段:串标题字节[]车身字节[]数据,从中我要计算一个哈希作为检查,以确保它们没有被篡改或损坏。

I have three fields: string Title, byte[] Body, and byte[] Data, from which I want to calculate a single hash as a check to be sure they haven't been tampered with or corrupted.

在Python中,我可以使用 md5.update()连续几次执行此。但我找不到在C#中类似的功能。要使用MD5.ComputeHash()我需要我的所有源复制到一个字节[],这是我想避免的一个步骤。

In Python, I can use md5.update() a few times in succession to perform this. But I can't find similar capability in C#. To use MD5.ComputeHash() I'd need to copy all my sources into a single byte[], which is a step I'd like to avoid.

如何我可以散列它一起到一个哈希,而无需将数据复制到一个临时缓冲区?

How can I hash it all together into one hash without having to copy the data into a temporary buffer?

推荐答案

几乎所有的散列算法设计的方式,它们可以连续地在多个块中的数据被馈送。结果是相同的,如果整个数据被一次散列

Almost all hash algorithms are designed in a way that they can successively be fed with the data in multiple blocks. The result is the same as if the whole data was hashed at once.

创建的例如一个实例 MD5CryptoServiceProvider 并调用的 TransformBlock方法每个块和的 TransformFinalBlock方法最后块:

Create an instance of e.g. MD5CryptoServiceProvider and call the TransformBlock Method for each block and the TransformFinalBlock Method for the last block:

MD5 md5 = new MD5CryptoServiceProvider();

// For each block:
md5.TransformBlock(block, 0, block.Length, block, 0);

// For last block:
md5.TransformFinalBlock(block, 0, block.Length);

// Get the hash code
byte[] hash = md5.Hash;

这篇关于散列多个字节[]的同时与C#一个哈希?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆