Exemple #1
0
        public void ComputeHashTest()
        {
            using Hash160 hash = new Hash160();
            byte[] data1     = Helper.HexToBytes("03306eeb63417d2e50c49bd5cb6256296116d6474c14853d64e008d281e392109a");
            byte[] actual1   = hash.ComputeHash(data1);
            byte[] expected1 = Helper.HexToBytes("3edd2f8b85027645ddb5aec9ad59b3b60c396c7e");

            byte[] data2     = Helper.HexToBytes("04306EEB63417D2E50C49BD5CB6256296116D6474C14853D64E008D281E392109AF3C0F0E015C966BE3DBB4BD09E4BE95EC109CCDFBEC4C4FD910E77091DC00A67");
            byte[] actual2   = hash.ComputeHash(data2);
            byte[] expected2 = Helper.HexToBytes("543e87f1cde0a028ad4c33afc8052ed78846c216");

            Assert.Equal(expected1, actual1);
            Assert.Equal(expected2, actual2);
        }
Exemple #2
0
        public void ComputeHashTest()
        {
            using Hash160 hash = new Hash160();
            byte[] data1     = Helper.HexToBytes(CompPub);
            byte[] actual1   = hash.ComputeHash(data1);
            byte[] expected1 = Helper.HexToBytes(CompPubHex);

            byte[] data2     = Helper.HexToBytes(UncompPub);
            byte[] actual2   = hash.ComputeHash(data2);
            byte[] expected2 = Helper.HexToBytes(UncompPubHex);

            Assert.Equal(expected1, actual1);
            Assert.Equal(expected2, actual2);
        }
Exemple #3
0
        private unsafe bool LoopComp(string key, int missingCount, char missingChar, byte[] expectedHash)
        {
            int[]  missingIndexes = new int[missingCount];
            byte[] ba             = new byte[32];
            for (int i = 0, j = 0; i < ba.Length; i++)
            {
                int hi, lo;
                if (key[i * 2] == missingChar)
                {
                    hi = 0;
                    missingIndexes[j++] = i * 2;
                }
                else
                {
                    hi = key[i * 2] - 65;
                    hi = hi + 10 + ((hi >> 31) & 7);
                }
                if (key[i * 2 + 1] == '*')
                {
                    lo = 0;
                    missingIndexes[j++] = i * 2 + 1;
                }
                else
                {
                    lo = key[i * 2 + 1] - 65;
                    lo = lo + 10 + ((lo >> 31) & 7) & 0x0f;
                }

                ba[i] = (byte)(lo | hi << 4);
            }

            var    cartesian = CartesianProduct.Create(Enumerable.Repeat(Enumerable.Range(0, 16), missingCount));
            ECCalc calc      = new ECCalc();


            BigInteger         smallVal = new BigInteger(ba, true, true);
            EllipticCurvePoint smallPub = calc.MultiplyByG(smallVal);
            bool foundAny = false;

            Parallel.ForEach(cartesian, (item, loopState) =>
            {
                Span <byte> temp = new byte[32];

                int mis = 0;
                foreach (int keyItem in item)
                {
                    int misIndex = missingIndexes[mis];
                    if (misIndex % 2 == 0)
                    {
                        temp[misIndex / 2] |= (byte)(keyItem << 4);
                    }
                    else
                    {
                        temp[misIndex / 2] |= (byte)keyItem;
                    }
                    mis++;
                }

                BigInteger tempVal         = new BigInteger(temp, true, true);
                EllipticCurvePoint tempPub = calc.MultiplyByG(tempVal);
                EllipticCurvePoint pub     = calc.AddChecked(tempPub, smallPub);
                byte[] toHash = new byte[33];
                toHash[0]     = pub.Y.IsEven ? (byte)2 : (byte)3;
                byte[] xBytes = pub.X.ToByteArray(true, true);
                Buffer.BlockCopy(xBytes, 0, toHash, 33 - xBytes.Length, xBytes.Length);

                Hash160 hash = new Hash160();
                ReadOnlySpan <byte> actual = hash.ComputeHash(toHash);
                if (actual.SequenceEqual(expectedHash))
                {
                    char[] origHex = key.ToCharArray();
                    int index      = 0;
                    foreach (var keyItem in item)
                    {
                        origHex[missingIndexes[index++]] = GetHex(keyItem);
                    }
                    report.AddMessageSafe($"Found a key: {new string(origHex)}");
                    foundAny = true;
                    loopState.Break();
                }
            });

            if (!foundAny)
            {
                report.AddMessageSafe("Failed to find any key.");
            }
            return(foundAny);
        }