适合的散列code方法的字节数组?数组、字节、适合、方法

2023-09-03 05:07:14 作者:满船清梦压星河

什么是最好的散列法对数组字节

What is the best hash method for an array of byte?

的阵列是包含在TCP / IP应用程序之间传递的JPEG图像序列的类对象。

The arrays are serialized class objects containing jpeg image passed between applications over TCP/IP.

数组大小为200K左右。

The array size is about 200k.

推荐答案

所有的内置哈希函数应该做的;这取决于你有多在乎冲突这些都是你的选择(从大多数碰撞到最低):

Any of the built-in hashing functions should do; depending on how much you care about collisions these are your options (from most collisions to least):

MD5 SHA1 SHA256 SHA384 SHA512

他们是那样简单易用:

var hash = SHA1.Create().ComputeHash(data);

加分:如果您不关心安全(我不认为你不给,你所得到的哈希值图像),你可能想看看杂音哈希,这设计用于内容散列和不安全散列(并且因此快得多)。 ,这是没有,不过在框架,所以你必须找到一种实现(和你应该去Murmur3)。

Bonus Marks: If you don't care about security (which I don't think you do given that you are getting the hashes for images) you might want to look into Murmur hash, which is designed for content hashing and not secure hashing (and is thus much faster). It isn't, however, in the framework so you will have to find an implementation (and you should probably go for Murmur3).

编辑:如果您正在寻找一个 HASH code 作为一个byte []数组它完全取决于你,它通常由位的移位(由素数)和异或运算。例如,

If you are looking for a HASHCODE for a byte[] array it's entirely up to you, it usually consists of bit shifting (by primes) and XORing. E.g.

public class ByteArrayEqualityComparer : IEqualityComparer<byte[]>
{
    public static readonly ByteArrayEqualityComparer Default = new ByteArrayEqualityComparer();
    private ByteArrayEqualityComparer() { }

    public bool Equals(byte[] x, byte[] y)
    {
        if (x == null && y == null)
            return true;
        if (x == null || y == null)
            return false;
        if (x.Length != y.Length)
            return false;
        for (var i = 0; i < x.Length; i++)
            if (x[i] != y[i])
                return false;
        return true;
    }

    public int GetHashCode(byte[] obj)
    {
        if (obj == null || obj.Length == 0)
            return 0;
        var hashCode = 0;
        for (var i = 0; i < obj.Length; i++)
            // Rotate by 3 bits and XOR the new value.
            hashCode = (hashCode << 3) | (hashCode >> (29)) ^ obj[i];
        return hashCode;
    }
}
// ...
var hc = ByteArrayEqualityComparer.Default.GetHashCode(data);

编辑:如果您想验证该值并没有改变,你应该使用的 CRC32 。

If you want to validate that the value hasn't changed you should use CRC32.