Example #1
0
        public static bool VerifyAuthorizerSignature(string message, string publicKey, string signature)
        {
            if (string.IsNullOrWhiteSpace(message) || !ValidatePublicKey(publicKey) || string.IsNullOrWhiteSpace(signature))
            {
                return(false);
            }
            var publicKeyBytes = Base58Encoding.DecodePublicKey(publicKey);

            return(VerifySignature(message, publicKeyBytes, signature));
        }
Example #2
0
        //public void GenarateKeyPair()
        //{
        //    var curve = ECNamedCurveTable.GetByName("secp256r1");
        //    var domainParams = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H, curve.GetSeed());
        //    var secureRandom = new SecureRandom();
        //    var keyParams = new ECKeyGenerationParameters(domainParams, secureRandom);
        //    var generator = new ECKeyPairGenerator("ECDSA");
        //    generator.Init(keyParams);
        //    var keyPair = generator.GenerateKeyPair();
        //    PrivateKey = keyPair.Private as ECPrivateKeyParameters;
        //    PublicKey = keyPair.Public as ECPublicKeyParameters;
        //}

        public static string GeneratePrivateKey()
        {
            var privateKey = new byte[32];
            var rnd        = System.Security.Cryptography.RandomNumberGenerator.Create();

            rnd.GetBytes(privateKey);
            //return Base58Encoding.Encode(privateKey);
            //return Base58Encoding.EncodeWithCheckSum(privateKey);
            return(Base58Encoding.EncodePrivateKey(privateKey));
        }
Example #3
0
        public static bool VerifyAccountSignature(string message, string accountId, string signature)
        {
            if (string.IsNullOrWhiteSpace(message) || !ValidateAccountId(accountId) || string.IsNullOrWhiteSpace(signature))
            {
                return(false);
            }
            var publicKeyBytes = Base58Encoding.DecodeAccountId(accountId);

            return(VerifySignature(message, publicKeyBytes, signature));
        }
Example #4
0
 public static bool ValidatePrivateKey(string PrivateKey)
 {
     try
     {
         Base58Encoding.DecodePrivateKey(PrivateKey);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
Example #5
0
            public override string ToString()
            {
                var b1   = R.ToByteArray();
                var b2   = S.ToByteArray();
                var buff = new byte[2 + b1.Length + b2.Length];

                buff[0] = (byte)b1.Length;
                buff[1] = (byte)b2.Length;
                Buffer.BlockCopy(b1, 0, buff, 2, b1.Length);
                Buffer.BlockCopy(b2, 0, buff, 2 + b1.Length, b2.Length);
                return(Base58Encoding.Encode(buff));
            }
Example #6
0
            public SignatureHolder(string signature)
            {
                var buff = Base58Encoding.Decode(signature);
                var b1   = new byte[buff[0]];
                var b2   = new byte[buff[1]];

                Buffer.BlockCopy(buff, 2, b1, 0, b1.Length);
                Buffer.BlockCopy(buff, 2 + b1.Length, b2, 0, b2.Length);

                R = new BigInteger(b1);
                S = new BigInteger(b2);
            }
Example #7
0
        private static byte[] DerivePublicKeyBytes(string privateKey)
        {
            var curve  = SecNamedCurves.GetByName("secp256r1");
            var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);

            byte[] pkbytes = Base58Encoding.DecodePrivateKey(privateKey);
            var    d       = new BigInteger(pkbytes);
            var    q       = domain.G.Multiply(d);

            var publicKey = new ECPublicKeyParameters(q, domain);

            return(publicKey.Q.GetEncoded());
        }
Example #8
0
        public static (string privateKey, string AccountId) GenerateWallet()
        {
            byte[] privateKey = new byte[32];
            using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(privateKey);
            }
            var kp = new KeyPair(privateKey);

            var pvtKeyStr = Base58Encoding.EncodePrivateKey(privateKey);

            var pubKey = kp.PublicKey.EncodePoint(false).Skip(1).ToArray();

            return(pvtKeyStr, Base58Encoding.EncodeAccountId(pubKey));
        }
Example #9
0
        public static bool ValidateAccountId(string AccountId)
        {
            try
            {
                if (AccountId[0] != 'L')
                {
                    return(false);
                }

                Base58Encoding.DecodeAccountId(AccountId);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
Example #10
0
        private static bool VerifySignature(string message, string AccountId, string signature)
        {
            try
            {
                var signatureBytes = Base58Encoding.Decode(signature);
                var publicKeyBytes = Base58Encoding.DecodeAccountId(AccountId);

                var result = Neo.Cryptography.Crypto.Default.VerifySignature(Encoding.UTF8.GetBytes(message), signatureBytes, publicKeyBytes);

                return(result);
            }
            catch (Exception ex)
            {
                _log?.LogError("VerifySignature failed: " + ex.Message);
                return(false);
            }
        }
        public static string GetSignature(string privateKey, string message, string AccountId)
        {
            var publicKeyBytes  = Base58Encoding.DecodeAccountId(AccountId);
            var privateKeyBytes = Base58Encoding.DecodePrivateKey(privateKey);

            //var signature = Neo.Cryptography.Crypto.Default.Sign(Encoding.UTF8.GetBytes(message), privateKeyBytes, publicKeyBytes);
            //return Base58Encoding.Encode(signature);

            Neo.Cryptography.ECC.ECDsa sa = new Neo.Cryptography.ECC.ECDsa(privateKeyBytes, Neo.Cryptography.ECC.ECCurve.Secp256r1);
            var sigInts = sa.GenerateSignature(Encoding.ASCII.GetBytes(message));

            var sh        = new SignatureHolder(sigInts);
            var signature = sh.ToString();

            //var vrt = VerifySignature(message, AccountId, signature);

            return(signature);
        }
        private static bool VerifySignature(string message, string AccountId, string signature)
        {
            try
            {
                var signatureBytes = Base58Encoding.Decode(signature);
                var publicKeyBytes = Base58Encoding.DecodeAccountId(AccountId);

                //return Neo.Cryptography.Crypto.Default.VerifySignature(Encoding.UTF8.GetBytes(message), signatureBytes, publicKeyBytes);
                Neo.Cryptography.ECC.ECDsa sa = new Neo.Cryptography.ECC.ECDsa(Neo.Cryptography.ECC.ECPoint.FromBytes(publicKeyBytes, Neo.Cryptography.ECC.ECCurve.Secp256r1));
                var sh = new SignatureHolder(signature);
                return(sa.VerifySignature(Encoding.ASCII.GetBytes(message), sh.R, sh.S));
            }
            catch (Exception ex)
            {
                _log.LogError("VerifySignature failed: " + ex.Message);
                return(false);
            }
        }
Example #13
0
        public static string GetSignature(string privateKey, string message)
        {
            var curve  = SecNamedCurves.GetByName("secp256r1");
            var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);

            //byte[] pkbytes = Base58Encoding.Decode(privateKey);
            //byte[] pkbytes = Base58Encoding.DecodeWithCheckSum(privateKey);
            byte[] pkbytes = Base58Encoding.DecodePrivateKey(privateKey);

            var keyParameters = new
                                ECPrivateKeyParameters(new Org.BouncyCastle.Math.BigInteger(1, pkbytes),
                                                       domain);

            ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");

            signer.Init(true, keyParameters);
            signer.BlockUpdate(Encoding.UTF8.GetBytes(message), 0, message.Length);
            var signature = signer.GenerateSignature();
            var netformat = SignatureHelper.ConvertDerToP1393(signature);

            return(Base58Encoding.Encode(netformat));
        }
Example #14
0
 public static string GetPublicKeyFromPrivateKey(string privateKey)
 {
     byte[] public_key_bytes = DerivePublicKeyBytes(privateKey);
     return(Base58Encoding.EncodePublicKey(public_key_bytes));
 }