Data.HashFunction icon indicating copy to clipboard operation
Data.HashFunction copied to clipboard

Different strings, but the xxHash calculation result is the same

Open gsw945 opened this issue 5 years ago • 1 comments

calculat hash for different unique string with xxHash algorithm, but got same result. c# code:

using System;
using System.Text;

using System.Data.HashFunction;
using System.Data.HashFunction.xxHash;

namespace DemoNS
{
    public class Demo
    {
        static void Main(string[] args)
        {
            uint v1 = CalcXXHash("cdkey-637302380103173928-f8830392-f3bf-4e92-aa73-6d8e9e6c0260-1199177810");
            Console.WriteLine("".PadLeft(50, '-'));
            uint v2 = CalcXXHash("cdkey-637302378177363195-42ce23ac-282f-4a8e-96c7-13a12d58c153-589858");
            return;
        }

        public static uint CalcXXHash(string origin)
        {
            IxxHash ixxHash = xxHashFactory.Instance.Create(new xxHashConfig()
            {
                HashSizeInBits = 32,
                Seed = 0
            });
            Console.WriteLine("origin: {0}", origin);
            byte[] byteData = Encoding.UTF8.GetBytes(origin);
            IHashValue hashValue = ixxHash.ComputeHash(byteData);
            Console.WriteLine("AsBase64String(): {0}", hashValue.AsBase64String());
            Console.WriteLine("AsHexString(): {0}", hashValue.AsHexString());
            byte[] hash = hashValue.Hash;
            string hashArray = string.Join(", ", Array.ConvertAll(hash, (byte item) => item.ToString()));
            Console.WriteLine("hashArray: {0}", hashArray);
            return BitConverter.ToUInt32(hash, 0);
        }
    }
}

result:

origin: cdkey-637302380103173928-f8830392-f3bf-4e92-aa73-6d8e9e6c0260-1199177810
AsBase64String(): 1Rt69Q==
AsHexString(): d51b7af5
hashArray: 213, 27, 122, 245
--------------------------------------------------
origin: cdkey-637302378177363195-42ce23ac-282f-4a8e-96c7-13a12d58c153-589858
AsBase64String(): 1Rt69Q==
AsHexString(): d51b7af5
hashArray: 213, 27, 122, 245

gsw945 avatar Jul 13 '20 06:07 gsw945

Yep, it's called a collision, and it's part of what an hash function is.

jodydonetti avatar Jul 13 '20 08:07 jodydonetti