简体   繁体   中英

Weird behaviour of MD5 hashing

I have faced aweird problem with the following code, the code below suppose to stop after one iteration, but it just keep going. However, if I remove the last "result_bytes = md5.ComputeHash(orig_bytes);" then it will work. Does anyone face similar problem before?

MD5 md5;
            byte[] orig_bytes;
            byte[] result_bytes;
            Dictionary<byte[], string> hashes = new Dictionary<byte[], string>();

            string input = "NEW YORK";
            result_bytes = UnicodeEncoding.Default.GetBytes("HELLO");
 while (!hashes.ContainsKey(result_bytes))
            {
                md5 = new MD5CryptoServiceProvider();
                orig_bytes = UnicodeEncoding.Default.GetBytes(input);
                result_bytes = md5.ComputeHash(orig_bytes);

                hashes.Add(result_bytes, input);
                Console.WriteLine(BitConverter.ToString(result_bytes));
                Console.WriteLine(hashes.ContainsKey(result_bytes));

                result_bytes = md5.ComputeHash(orig_bytes);
            }

当您将result_bytes重新分配给最后一行中的新值时,您有一个对字节数组的新引用,该数组不等于集合中的数组,因此hashes.ContainsKey返回false。

You're assuming that byte arrays override Equals and GetHashCode to compare for equality: they don't. They just use the default identity test - so without the extra assignment at the end, you're just checking whether the exact key object you've just added is still in the dictionary - which of course it is.

One way round this would be to store a reversible string representation of the hash (eg using base64), instead of the hash itself. Or write your own implementation of IEqualityComparer<byte[]> and pass that to the Dictionary constructor, so that it uses that implementation to find the hash code of byte arrays and compare them with each other.

In short: this has nothing to do with MD5, and everything to do with the fact that

Console.WriteLine(new byte[0].Equals(new byte[0]));

will print False :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM