BattleNet.Bsha1.GetHash C# (CSharp) Метод

GetHash() публичный статический Метод

public static GetHash ( List input ) : List
input List
Результат List
        public static List<byte> GetHash(List<byte> input)
        {
            uint[] buffer = new uint[21];
            buffer[0] = 0x67452301;
            buffer[1] = 0xEFCDAB89;
            buffer[2] = 0x98BADCFE;
            buffer[3] = 0x10325476;
            buffer[4] = 0xC3D2E1F0;

            uint max_subsection_length = 64;
            uint initialized_length = 20;

            for(uint i = 0 ; i < input.Count ; i += max_subsection_length)
            {
                uint subsection_length = Math.Min(max_subsection_length,(uint)input.Count-i);

                if (subsection_length > max_subsection_length)
                    subsection_length = max_subsection_length;

                for (uint j = 0; j < subsection_length; j++)
                {
                    byte[] temp = new byte[input.Count];
                    input.CopyTo(temp);
                    setBufferByte(buffer, (int)(initialized_length + j), temp[(int)(j + i)]);
                }

                if(subsection_length < max_subsection_length)
                {
                    for(uint j = subsection_length ; j < max_subsection_length ; j++)
                        setBufferByte(buffer, (int)(initialized_length+j),0);
                }

                CalculateHash(ref buffer);

            }

            List<byte> op = new List<byte>();
            for (uint i = 0; i < buffer.Length; i++)
                for (uint j = 0; j < 4; j++)
                    op.Add(getBufferByte(buffer, (int)(i * 4 + j)));
            return new List<byte>(op.GetRange(0,20));
        }

Usage Example

Пример #1
0
        public static bool GetD2KeyHash(string cdkey, ref uint clientToken, uint serverToken, ref List <byte> output, ref List <byte> publicValue)
        {
            ulong checksum = 0;
            ulong n, n2, v, v2;
            char  c1, c2, c;

            String manipulatedKey = cdkey;

            for (int i = 0; i < cdkey.Length; i += 2)
            {
                char[] tmpBuffer = manipulatedKey.ToCharArray();
                c1 = (char)AlphaMap[cdkey[i]];
                n  = (ulong)c1 * 3;
                c2 = (char)AlphaMap[cdkey[i + 1]];
                n  = (ulong)c2 + 8 * n;

                if (n >= 0x100)
                {
                    n -= 0x100;
                    ulong temp = (ulong)1 << (i >> 1);
                    checksum |= temp;
                }
                n2               = n;
                n2             >>= 4;
                tmpBuffer[i]     = ConvertToHexDigit(n2);
                tmpBuffer[i + 1] = ConvertToHexDigit(n);

                manipulatedKey = new string(tmpBuffer);
            }

            v = 3;

            for (int i = 0; i < 16; i++)
            {
                n  = ConvertFromHexDigit(manipulatedKey[i]);
                n2 = v * 2;
                n ^= n2;
                v += n;
            }

            v &= 0xFF;
            if (v != checksum)
            {
                return(false);
            }

            for (int i = 15; i >= 0; i--)
            {
                c = manipulatedKey[i];
                if (i > 8)
                {
                    n = (ulong)i - 9;
                }
                else
                {
                    n = 0xF - (ulong)(8 - i);
                }
                n &= 0xF;

                c2 = manipulatedKey[(int)n];
                char[] tmpBuffer = manipulatedKey.ToCharArray();
                tmpBuffer[i]   = c2;
                tmpBuffer[n]   = c;
                manipulatedKey = new string(tmpBuffer);
            }

            v2 = 0x13AC9741;

            for (int i = 15; i >= 0; i--)
            {
                c = char.ToUpper(manipulatedKey[i]);
                char[] tmpBuffer = manipulatedKey.ToCharArray();
                tmpBuffer[i] = c;


                if (c <= '7')
                {
                    v            = v2;
                    c2           = (char)(v & 0xFF);
                    c2          &= (char)7;
                    c2          ^= c;
                    v          >>= 3;
                    tmpBuffer[i] = c2;
                    v2           = v;
                }
                else if (c < 'A')
                {
                    c2           = (char)i;
                    c2          &= (char)1;
                    c2          ^= c;
                    tmpBuffer[i] = c2;
                }
                manipulatedKey = new string(tmpBuffer);
            }

            string hexString = manipulatedKey.Substring(2, 6);
            UInt32 num       = UInt32.Parse(hexString, System.Globalization.NumberStyles.HexNumber);

            publicValue = new List <byte>(BitConverter.GetBytes(num));

            List <byte> hashData = new List <byte>(BitConverter.GetBytes(clientToken));

            hashData.AddRange(BitConverter.GetBytes(serverToken));


            hashData.AddRange(BitConverter.GetBytes(UInt32.Parse(manipulatedKey.Substring(0, 2), System.Globalization.NumberStyles.HexNumber)));

            hashData.AddRange(BitConverter.GetBytes(num));
            hashData.AddRange(BitConverter.GetBytes((int)0));
            hashData.AddRange(BitConverter.GetBytes(UInt32.Parse(manipulatedKey.Substring(8, 8), System.Globalization.NumberStyles.HexNumber)));

            output = Bsha1.GetHash(hashData);

            return(true);
        }