示例#1
0
        public static string GetAddressOfcompressed(BigInteger[] publicKey, bool show)
        {
            var publicKeyArray1 = HexToByteArray.BigIntegerTo32ByteArray(publicKey[0]);

            HexToByteArray.ChangeDirection(ref publicKeyArray1);
            // var publicKeyArray2 = HexToByteArray.BigIntegerTo32ByteArray(publicKey[1]);

            byte[] resultAdd;
            if (publicKey[1].IsEven)
            {
                resultAdd = Calculate.BiteSplitJoint(new byte[] { 0x02 }, publicKeyArray1);
            }
            else
            {
                resultAdd = Calculate.BiteSplitJoint(new byte[] { 0x03 }, publicKeyArray1);
            }

            if (show)
            {
                Console.WriteLine($"压缩公钥为{ Hex.BytesToHex(resultAdd)}");
            }
            //   Console.WriteLine($"压缩公钥为{ Calculate.Encode(resultAdd)}");

            var step3 = ripemd160.ComputeHash(sha256.ComputeHash(resultAdd));

            var step4 = Calculate.BiteSplitJoint(new byte[] { 0x00 }, step3);

            var step5 = sha256.ComputeHash(sha256.ComputeHash(step4));

            var step6 = Calculate.BiteSplitJoint(step4, new byte[] { step5[0], step5[1], step5[2], step5[3] });

            return(Calculate.Encode(step6));
        }
示例#2
0
        public static string GetAddressOfUncompressed(BigInteger[] publicKey)
        {
            var publicKeyArray1 = HexToByteArray.BigIntegerTo32ByteArray(publicKey[0]);

            HexToByteArray.ChangeDirection(ref publicKeyArray1);
            var publicKeyArray2 = HexToByteArray.BigIntegerTo32ByteArray(publicKey[1]);

            HexToByteArray.ChangeDirection(ref publicKeyArray2);
            //    var array = HexToByteArray.BigIntegerTo32ByteArray(privateKey);

            var resultAdd = Calculate.BiteSplitJoint(new byte[] { 0x04 }, publicKeyArray1);

            resultAdd = Calculate.BiteSplitJoint(resultAdd, publicKeyArray2);
            Console.WriteLine($"非压缩公钥为{ Hex.BytesToHex(resultAdd)}");


            var step3 = ripemd160.ComputeHash(sha256.ComputeHash(resultAdd));

            var step4 = Calculate.BiteSplitJoint(new byte[] { 0x00 }, step3);

            var step5 = sha256.ComputeHash(sha256.ComputeHash(step4));

            var step6 = Calculate.BiteSplitJoint(step4, new byte[] { step5[0], step5[1], step5[2], step5[3] });

            return(Calculate.Encode(step6));

            //SHA256 sha256 = new SHA256Managed();
            //byte[] hash1 = sha256.ComputeHash(resultAdd);

            //var pubKeyHash = Hash160.Hash(publicKey.EncodePoint(hash1));
            //byte[] hash2 = sha256.ComputeHash(hash1);

            //var result = new byte[CheckSumSizeInBytes];
            //Buffer.BlockCopy(hash2, 0, result, 0, result.Length);

            //byte[] chechHash = Calculate.GetCheckSum(resultAdd);
        }
示例#3
0
文件: Sign.cs 项目: sxtgyrq/VRPGame
        public static string SignMessage()
        {
            Console.WriteLine("请输入要签名的信息(utf-8)");
            var msg = Console.ReadLine();

            Console.WriteLine("请输入要私钥");
            //   output($"拖入您的Base58编码的37位或38位的私钥的路径,用此私钥进行验证.即校验.txt");

            var privateKey = Console.ReadLine();

            System.Numerics.BigInteger privateBigInteger;
            if (PrivateKeyF.Check(privateKey, out privateBigInteger))
            {
            }
            else
            {
                Console.WriteLine($"请输入正确的私钥!!!");
                return("");
            }
            bool   compressed;
            string address;

            if (privateKey.Length == 51)
            {
                compressed = false;
                address    = PublicKeyF.GetAddressOfUncompressed(Calculate.getPublicByPrivate(privateBigInteger));
            }
            else if (privateKey.Length == 52)
            {
                compressed = true;
                address    = PublicKeyF.GetAddressOfcompressed(Calculate.getPublicByPrivate(privateBigInteger));
            }
            else
            {
                throw new Exception("");
            }


            var msgHash = msg_digest(msg);
            var signal  = PrivateKeyF.Sign(privateBigInteger, msgHash);
            var r       = signal[0];
            var rByte   = HexToByteArray.BigIntegerTo32ByteArray(r);

            HexToByteArray.ChangeDirection(ref rByte);

            var s     = signal[1];
            var sByte = HexToByteArray.BigIntegerTo32ByteArray(s);

            HexToByteArray.ChangeDirection(ref sByte);

            // Console.WriteLine($"{Convert.ToBase64String(rByte)}{Convert.ToBase64String(sByte)}");

            var sequence = new byte[] { 0 }; //[0];

            sequence = sequence.Concat(rByte).ToArray();
            sequence = sequence.Concat(sByte).ToArray();

            for (var i = 0; i < 4; i++)
            {
                int nV = 27 + i;
                if (compressed)
                {
                    nV += 4;
                }
                sequence[0] = Convert.ToByte(nV);
                var sig = Convert.ToBase64String(sequence);
                Console.WriteLine(sig);
                var calAddredd = verify_message(sig, msg, compressed ? 1 : 0);
                // return sig;
                if (calAddredd == address)
                {
                    return(sig);
                }
                //if (verify_message(sig, msg, compressed ? 1 : 0) == address)
                //    return sig;
            }
            return("");
        }