本文介绍了C#的字节数组或图像创建一个散列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:结果
的一个字节数组的哈希码

在C#中,我需要创建一个图像的哈希,以确保它是存储独一无二的。

In C#, I need to create a Hash of an image to ensure it is unique in storage.

我可以很容易地将其转换为一个字节数组,但不确定如何从那里继续。

I can easily convert it to a byte array, but unsure how to proceed from there.

有没有在.NET框架的任何类,可以帮助我,或者是任何人都了解一些的有效的算法来创建这样一个唯一的哈希?

Are there any classes in the .NET framework that can assist me, or is anyone aware of some efficient algorithms to create such a unique hash?

推荐答案

有大量的哈希和供应商在.NET中,营造密码散列 - 这satisifies你的病情,他们是唯一的(大多数情况下碰撞的证明)。他们都是的非常的快,哈希,除非你在做一个万亿次绝对不会在你的应用程序的瓶颈。

There's plenty of hashsum providers in .NET which create cryptographic hashes - which satisifies your condition that they are unique (for most purposes collision-proof). They are all extremely fast and the hashing definitely won't be the bottleneck in your app unless you're doing it a trillion times over.

我个人喜欢SHA1:

string hash;
using(SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
    hash = Convert.ToBase64String(sha1.ComputeHash(byteArray));
}



即使当人们说一种方法可能比另一种更慢,这一切都在相对条款。处理图像的绝对的程序不会注意到生成哈希和的微秒过程。

Even when people say one method might be slower than another, it's all in relative terms. A program dealing with images definitely won't notice the microsecond process of generating a hashsum.

和关于碰撞,在大多数情况下,这也是无关紧要的。即使是过时之类MD5方法仍然在大多数情况下是非常有用的。只有当你的系统的安全性的依赖于预防冲突

And regarding collisions, for most purposes this is also irrelevant. Even "obsolete" methods like MD5 are still highly useful in most situations. Only recommend not using it when the security of your system relies on preventing collisions.

这篇关于C#的字节数组或图像创建一个散列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 16:47