public static string ToBase58String(byte[] data)
        {
            Org.BouncyCastle.Math.BigInteger toConvertToBase58 = new Org.BouncyCastle.Math.BigInteger(1, data);

            Org.BouncyCastle.Math.BigInteger big0  = new Org.BouncyCastle.Math.BigInteger("0");
            Org.BouncyCastle.Math.BigInteger big58 = new Org.BouncyCastle.Math.BigInteger("58");

            StringBuilder base58Builder = new StringBuilder();

            while (toConvertToBase58.CompareTo(big0) > 0)
            {
                int d = Convert.ToInt32(toConvertToBase58.Mod(big58).ToString());
                toConvertToBase58 = toConvertToBase58.Divide(big58);

                base58Builder.Insert(0, b58.Substring(d, 1));
            }

            // handle leading zeroes
            foreach (byte b in data)
            {
                if (b != 0)
                {
                    break;
                }

                base58Builder.Insert(0, leadingZeroCharacter);
            }

            return(base58Builder.ToString());
        }
Ejemplo n.º 2
0
        private string ByteArrayToBase58(byte[] ba)
        {
            Org.BouncyCastle.Math.BigInteger addrremain = new Org.BouncyCastle.Math.BigInteger(1, ba);

            Org.BouncyCastle.Math.BigInteger big0  = new Org.BouncyCastle.Math.BigInteger("0");
            Org.BouncyCastle.Math.BigInteger big58 = new Org.BouncyCastle.Math.BigInteger("58");

            string b58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

            string rv = "";

            while (addrremain.CompareTo(big0) > 0)
            {
                int d = System.Convert.ToInt32(addrremain.Mod(big58).ToString());
                addrremain = addrremain.Divide(big58);
                rv         = b58.Substring(d, 1) + rv;
            }

            // handle leading zeroes
            foreach (byte b in ba)
            {
                if (b != 0)
                {
                    break;
                }
                rv = "1" + rv;
            }
            return(rv);
        }
Ejemplo n.º 3
0
        public static string FromByteArray(byte[] ba)
        {
            Org.BouncyCastle.Math.BigInteger addrremain = new Org.BouncyCastle.Math.BigInteger(1, ba);

            Org.BouncyCastle.Math.BigInteger big0 = new Org.BouncyCastle.Math.BigInteger("0");
            Org.BouncyCastle.Math.BigInteger big58 = new Org.BouncyCastle.Math.BigInteger("58");

            string b58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";

            string rv = "";

            while (addrremain.CompareTo(big0) > 0) {
                int d = Convert.ToInt32(addrremain.Mod(big58).ToString());
                addrremain = addrremain.Divide(big58);
                rv = b58.Substring(d, 1) + rv;
            }

            // handle leading zeroes
            foreach (byte b in ba) {
                if (b != 0) break;
                rv = "1" + rv;

            }
            return rv;
        }
Ejemplo n.º 4
0
        public static Org.BouncyCastle.Math.BigInteger EnforceLowS(Org.BouncyCastle.Math.BigInteger s)
        {
            // If it's large we set it as N - S.
            if (s.CompareTo(_b_halfN) > 0)
            {
                return(Parameters.N.Subtract(s));
            }

            // Otherwise we simply return it.
            return(s);
        }
Ejemplo n.º 5
0
Archivo: Seed.cs Proyecto: Ibasa/Ripple
        private void Secp256k1KeyPair(out Secp256k1KeyPair rootKeyPair, out Secp256k1KeyPair keyPair)
        {
            Span <byte> rootSource = stackalloc byte[20];

            UnsafeAsSpan(ref this).CopyTo(rootSource);

            var secpSecretBytes = new byte[32];
            var k1Params        = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");

            using (var sha512 = System.Security.Cryptography.SHA512.Create())
            {
                Span <byte> destination = stackalloc byte[64];

                uint i;
                Org.BouncyCastle.Math.BigInteger secpRootSecret = default;
                for (i = 0; i < uint.MaxValue; ++i)
                {
                    System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(rootSource.Slice(16), i);
                    var done = sha512.TryComputeHash(rootSource, destination, out var bytesWritten);
                    destination.Slice(0, 32).CopyTo(secpSecretBytes);

                    secpRootSecret = new Org.BouncyCastle.Math.BigInteger(1, secpSecretBytes);
                    if (secpRootSecret.CompareTo(Org.BouncyCastle.Math.BigInteger.Zero) == 1 && secpRootSecret.CompareTo(k1Params.N) == -1)
                    {
                        break;
                    }
                }

                rootKeyPair = new Secp256k1KeyPair(secpRootSecret);

                // Calculate intermediate
                Span <byte> intermediateSource = stackalloc byte[41];
                rootKeyPair.PublicKey.GetCanoncialBytes().CopyTo(intermediateSource);
                System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(intermediateSource.Slice(33), 0);

                Org.BouncyCastle.Math.BigInteger secpIntermediateSecret = default;
                for (i = 0; i < uint.MaxValue; ++i)
                {
                    System.Buffers.Binary.BinaryPrimitives.WriteUInt32BigEndian(intermediateSource.Slice(37), i);
                    var done = sha512.TryComputeHash(intermediateSource, destination, out var bytesWritten);
                    destination.Slice(0, 32).CopyTo(secpSecretBytes);

                    secpIntermediateSecret = new Org.BouncyCastle.Math.BigInteger(1, secpSecretBytes);
                    if (secpIntermediateSecret.CompareTo(Org.BouncyCastle.Math.BigInteger.Zero) == 1 && secpIntermediateSecret.CompareTo(k1Params.N) == -1)
                    {
                        break;
                    }
                }

                var masterPrivateKey = secpRootSecret.Add(secpIntermediateSecret).Mod(k1Params.N);
                keyPair = new Secp256k1KeyPair(masterPrivateKey);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Check is the key is valid see RFC 2631, Section 2.1.5, http://www.ietf.org/rfc/rfc2631.txt
        /// </summary>
        public static bool IsValidPublicKey(Org.BouncyCastle.Math.BigInteger y, Org.BouncyCastle.Math.BigInteger p, Org.BouncyCastle.Math.BigInteger q)
        {
            Org.BouncyCastle.Math.BigInteger bn;

            // y must lie in [2,p-1]
            // check y < 2 then failed
            bn = new Org.BouncyCastle.Math.BigInteger("2");
            if (y.CompareTo(bn) < 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key must be at least 2");
                return(false);
            }

            // y must lie in [2,p-1]
            bn = new Org.BouncyCastle.Math.BigInteger(p.ToString());
            bn = bn.Subtract(new Org.BouncyCastle.Math.BigInteger("1"));
            if (y.CompareTo(bn) > 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key must be at most p-2");
                return(false);
            }


            // Verify with Sophie-Germain prime
            //
            // This is a nice test to make sure the public key position is calculated
            // correctly. This test will fail in about 50% of the cases if applied to
            // random data.
            bn = y.ModPow(q, p);
            if (bn.CompareTo(new Org.BouncyCastle.Math.BigInteger("1")) != 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key does not fulfill y^q mod p = 1");
                return(false);
            }

            return(true);
        }
Ejemplo n.º 7
0
        private void Mine()
        {
            MinerInfo.minerStillRunning = true;


            var fromBiginteger  = new Org.BouncyCastle.Math.BigInteger(Helper.ConvertBigintegerHexToDecimal(from, hex));
            var untilBiginteger = new Org.BouncyCastle.Math.BigInteger(Helper.ConvertBigintegerHexToDecimal(until, hex));
            var difference      = fromBiginteger.Subtract(untilBiginteger).Abs();

            if (Helper.CompareNumbers(difference.ToString(), long.MaxValue.ToString()) > 0)
            {
                System.Windows.Forms.MessageBox.Show($"The length of job cant be more than {long.MaxValue}! Please use a smaller job length!");
                MinerInfo.minerStillRunning = false;
                return;
            }
            MinerInfo.lengthOfJob = Math.Abs(untilBiginteger.LongValue - fromBiginteger.LongValue);
            Task.Factory.StartNew(() =>
            {
                MinerInfo.minerThreadInfo = "Lookup size determine,generating hashset...";

                lookupSet = new HashSet <string>(10000);

                using (StreamReader sr = new StreamReader(path))
                {
                    while (sr.Peek() != -1)
                    {
                        lookupSet.Add(sr.ReadLine());
                    }
                }
                stopwatch.Start();
                MinerInfo.minerThreadInfo = "";
                MinerInfo.minerThreadInfo = "Lookup size determine, generating hashset done, mining...";

                while (fromBiginteger.CompareTo(untilBiginteger) < 0 && increment || fromBiginteger.CompareTo(untilBiginteger) > 0 && !increment)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        break;
                    }
                    var key = KeyPair.Create(fromBiginteger, compressed);
                    if (lookupSet.Contains(key.AddressBase58))
                    {
                        MinerInfo.minerThreadInfo = $"Found key!!!! Number: {fromBiginteger.ToString()}, address: {key.AddressBase58}";
                        foundKeyCount++;
                        if (form.InvokeRequired)
                        {
                            form.Invoke((MethodInvoker) delegate { textBox.Text += $"{Environment.NewLine}Found key!!!! Number: {fromBiginteger.ToString()}, address: {key.AddressBase58}, privatekey: {key.PrivateKey}{Environment.NewLine}"; });
                        }
                        else
                        {
                        }
                        StreamWriter streamWriter = new StreamWriter("keys.txt", true);
                        string keyline            = $"Found key!!!! Number: {fromBiginteger.ToString()}, address: {key.AddressBase58}, privatekey: {key.PrivateKey}";
                        streamWriter.WriteLine(keyline);
                        streamWriter.Flush();
                        streamWriter.Close();
                    }
                    if (increment)
                    {
                        fromBiginteger = fromBiginteger.Add(new Org.BouncyCastle.Math.BigInteger("1"));
                    }
                    else
                    {
                        fromBiginteger = fromBiginteger.Add(new Org.BouncyCastle.Math.BigInteger("-1"));
                    }
                    MinerInfo.currentlyProcessed++;
                    MinerInfo.countOfTriedKeys++;
                    if (MinerInfo.currentlyProcessed % 1000 == 0)
                    {
                        MinerInfo.minerThreadInfo = $"Currently Processed number: {fromBiginteger.ToString()}";
                    }
                }
                //int lengthOfOneJob = 0;
                //if (MinerInfo.lengthOfJob > 10000)
                //{
                //    lengthOfOneJob = 10000;
                //}
                //else
                //{
                //    lengthOfOneJob = (int)MinerInfo.lengthOfJob;
                //}
                //StringBuilder stringBuilder = new StringBuilder(chars.Length);
                //for (int i = 0; i < chars.Length; i++)
                //{
                //    stringBuilder.Append(chars[0]);
                //}
                //List<string> sequenceList = new List<string>(lengthOfOneJob);
                //var list = GetAllMatches(chars.ToCharArray(), lengthOfString);
                //foreach (var item in list)
                //{
                //    if (cancellationToken.IsCancellationRequested)
                //    {
                //        break;
                //    }
                //    sequenceList.Add(item);
                //    if (sequenceList.Count == lengthOfOneJob)
                //    {
                //        LookupKeys(sequenceList);
                //        sequenceList.Clear();
                //    }
                //    MinerInfo.currentlyProcessed++;
                //    if (MinerInfo.currentlyProcessed >= MinerInfo.lengthOfJob && MinerInfo.lengthOfJob > 10000)
                //    {
                //        LookupKeys(sequenceList);
                //        break;
                //    }
                //}
                //int count = list.Count();

                MinerInfo.minerThreadResults = $"{Environment.NewLine}Found keys: {foundKeyCount}, ckecked numbers: {MinerInfo.countOfTriedKeys} {Environment.NewLine}Additional data: - Combinations: {MinerInfo.lengthOfJob}, elapsed time: {stopwatch.Elapsed}, keys/second: {MinerInfo.countOfTriedKeys / stopwatch.Elapsed.TotalSeconds}";
                Dispose();
                MinerInfo.minerStillRunning = false;
            }, TaskCreationOptions.LongRunning);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Check is the key is valid see RFC 2631, Section 2.1.5, http://www.ietf.org/rfc/rfc2631.txt
        /// </summary>
        public static bool IsValidPublicKey(Org.BouncyCastle.Math.BigInteger y, Org.BouncyCastle.Math.BigInteger p, Org.BouncyCastle.Math.BigInteger q)
        {
            Org.BouncyCastle.Math.BigInteger bn;

            // y must lie in [2,p-1]
            // check y < 2 then failed
            bn = new Org.BouncyCastle.Math.BigInteger("2");
            if (y.CompareTo(bn) < 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key must be at least 2");
                return false;
            }

            // y must lie in [2,p-1]
            bn = new Org.BouncyCastle.Math.BigInteger(p.ToString());
            bn = bn.Subtract(new Org.BouncyCastle.Math.BigInteger("1"));
            if (y.CompareTo(bn) > 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key must be at most p-2");
                return false;
            }


            // Verify with Sophie-Germain prime
            //
            // This is a nice test to make sure the public key position is calculated
            // correctly. This test will fail in about 50% of the cases if applied to
            // random data.
            bn = y.ModPow(q, p);
            if (bn.CompareTo(new Org.BouncyCastle.Math.BigInteger("1")) != 0)
            {
                LibRTMPLogger.Log(LibRTMPLogLevel.Warning, "[CDR.LibRTMP.RTMPHelper.IsValidPublicKey] DH public key does not fulfill y^q mod p = 1");
                return false;
            }

            return true;
        }
Ejemplo n.º 9
0
 public static bool CheckLowS(Org.BouncyCastle.Math.BigInteger s)
 {
     // Check that s is low.
     return(s.CompareTo(_b_halfN) < 0);
 }