brandondahler / Data.HashFunction

C# library to create a common interface to non-cryptographic hash functions.
MIT License
255 stars 42 forks source link

Different strings, but the xxHash calculation result is the same #57

Open gsw945 opened 4 years ago

gsw945 commented 4 years ago

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
jodydonetti commented 4 years ago

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