Skip to content
This repository has been archived by the owner on May 13, 2022. It is now read-only.

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

Open
gsw945 opened this issue Jul 13, 2020 · 1 comment
Open

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

gsw945 opened this issue Jul 13, 2020 · 1 comment

Comments

@gsw945
Copy link

gsw945 commented Jul 13, 2020

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
Copy link

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants