예제 #1
0
        public static byte[] GenerateHash(byte[] byteMessage)
        {
            uint[] H = new uint[] { 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19 }; // Base rests array

            byte [] preparedArray = PrepareByteArray(byteMessage);                                                                    // Array with paddings

            List <uint[]> uintPartsList = SplitAndConvert(preparedArray);                                                             // Split list of arrays converted to uint

            foreach (uint[] uintPart in uintPartsList)
            {
                uint[] W = new uint[64];
                W.AddRangeToFront(uintPart);

                ExpandTo64Uints(ref W); // Expand from 16 to 64 uints

                uint a = H[0];
                uint b = H[1];
                uint c = H[2];
                uint d = H[3];
                uint e = H[4];
                uint f = H[5];
                uint g = H[6];
                uint h = H[7];

                uint s0, s1, t1, t2, ch, maj;
                for (int i = 0; i < 64; i++)
                {
                    s1 = e.RR(6) ^ e.RR(11) ^ e.RR(25);
                    ch = (e & f) ^ ((~e) & g);
                    t1 = h + s1 + ch + K[i] + W[i];

                    s0  = a.RR(2) ^ a.RR(13) ^ a.RR(22);
                    maj = (a & b) ^ (a & c) ^ (b & c);
                    t2  = s0 + maj;

                    h = g;
                    g = f;
                    f = e;
                    e = d + t1;
                    d = c;
                    c = b;
                    b = a;
                    a = t1 + t2;
                }

                H[0] += a;
                H[1] += b;
                H[2] += c;
                H[3] += d;
                H[4] += e;
                H[5] += f;
                H[6] += g;
                H[7] += h;
            }

            if (ILE)
            {
                return(H.SelectMany(sm => BitConverter.GetBytes(sm).ReverseToByte()).ToArray());
            }
            else
            {
                return(H.SelectMany(sm => BitConverter.GetBytes(sm)).ToArray());
            }
        }