Exemplo n.º 1
0
        public unsafe void Compress30Test()
        {
            int dataLen = 30;

            byte[] data     = GetRandomBytes(dataLen);
            byte[] expected = ComputeSingleSha(data);

            using Sha256Fo sha = new Sha256Fo();
            fixed(uint *hPt = &sha.hashState[0], wPt = &sha.w[0])
            {
                int dIndex = 0;

                for (int i = 0; i < 7; i++, dIndex += 4)
                {
                    wPt[i] = (uint)((data[dIndex] << 24) | (data[dIndex + 1] << 16) | (data[dIndex + 2] << 8) | data[dIndex + 3]);
                }
                wPt[7] = (uint)((data[28] << 24) | (data[29] << 16) | 0b00000000_00000000_10000000_00000000U);

                wPt[15] = (uint)dataLen * 8;
                sha.Init(hPt);
                sha.Compress30(hPt, wPt);
                byte[] actual = sha.GetBytes(hPt);

                Assert.Equal(expected, actual);
            }
        }
Exemplo n.º 2
0
        private unsafe bool Loop31()
        {
            var cartesian = CartesianProduct.Create(Enumerable.Repeat(Encoding.UTF8.GetBytes(ConstantsFO.Base58Chars), missCount));

            using Sha256Fo sha = new Sha256Fo();
            bool success = false;

            byte[] temp = new byte[precomputed.Length];
            fixed(uint *hPt = &sha.hashState[0], wPt = &sha.w[0])
            fixed(byte *pre = &precomputed[0], tmp = &temp[0])
            fixed(int *mi   = &missingIndexes[0])
            {
                foreach (var item in cartesian)
                {
                    Buffer.MemoryCopy(pre, tmp, 30, 30);
                    int mis = 0;
                    foreach (var keyItem in item)
                    {
                        tmp[mi[mis]] = keyItem;
                        mis++;
                    }

                    // The added value below is the fixed first char(S)=0x53 shifted left 24 places
                    wPt[0] = 0b01010011_00000000_00000000_00000000U | (uint)tmp[1] << 16 | (uint)tmp[2] << 8 | tmp[3];
                    wPt[1] = (uint)tmp[4] << 24 | (uint)tmp[5] << 16 | (uint)tmp[6] << 8 | tmp[7];
                    wPt[2] = (uint)tmp[8] << 24 | (uint)tmp[9] << 16 | (uint)tmp[10] << 8 | tmp[11];
                    wPt[3] = (uint)tmp[12] << 24 | (uint)tmp[13] << 16 | (uint)tmp[14] << 8 | tmp[15];
                    wPt[4] = (uint)tmp[16] << 24 | (uint)tmp[17] << 16 | (uint)tmp[18] << 8 | tmp[19];
                    wPt[5] = (uint)tmp[20] << 24 | (uint)tmp[21] << 16 | (uint)tmp[22] << 8 | tmp[23];
                    wPt[6] = (uint)tmp[24] << 24 | (uint)tmp[25] << 16 | (uint)tmp[26] << 8 | tmp[27];
                    // The added value below is the SHA padding and the last added ? char equal to 0x3f shifted right 8 places
                    wPt[7] = (uint)tmp[28] << 24 | (uint)tmp[29] << 16 | 0b00000000_00000000_00111111_10000000U;
                    // from 8 to 14 = 0
                    wPt[15] = 248; // 31 *8 = 184

                    sha.Init(hPt);
                    sha.Compress31(hPt, wPt);

                    if ((hPt[0] & 0b11111111_00000000_00000000_00000000U) == 0)
                    {
                        // Same as above
                        wPt[7] ^= 0b00000000_00000000_10111111_10000000U;
                        // from 8 to 14 (remain) = 0
                        wPt[15] = 240; // 30 *8 = 240

                        sha.Init(hPt);
                        sha.Compress30(hPt, wPt);

                        if (comparer.Compare(sha.GetBytes(hPt)))
                        {
                            SetResult(item);
                            success = true;
                            break;
                        }
                    }
                }
            }

            return(success);
        }