Пример #1
0
        public void TestVerifySignature()
        {
            ECDsa sa = new ECDsa(key.PrivateKey, key.PublicKey.Curve);

            byte[]       message = System.Text.Encoding.Default.GetBytes("HelloWorld");
            BigInteger[] result  = sa.GenerateSignature(message);
            sa.VerifySignature(message, result[0], result[1]).Should().BeTrue();
            sa.VerifySignature(message, new BigInteger(-100), result[1]).Should().BeFalse();
        }
Пример #2
0
        public void TestGenerateSignature()
        {
            ECDsa sa = new ECDsa(key.PrivateKey, key.PublicKey.Curve);

            byte[] message = System.Text.Encoding.Default.GetBytes("HelloWorld");
            for (int i = 0; i < 30; i++)
            {
                BigInteger[] result = sa.GenerateSignature(message);
                result.Length.Should().Be(2);
            }
            sa = new ECDsa(key.PublicKey);
            Action action = () => sa.GenerateSignature(message);

            action.ShouldThrow <InvalidOperationException>();
        }
        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);
            }
        }