Example #1
0
        public static bool VerifySignature(byte[] message, byte[] signature, byte[] pubkey)
        {
            //unity dotnet不完整,不能用dotnet自带的ecdsa
            var PublicKey = ThinNeo.Cryptography.ECC.ECPoint.DecodePoint(pubkey, ThinNeo.Cryptography.ECC.ECCurve.Secp256r1);
            var ecdsa     = new ThinNeo.Cryptography.ECC.ECDsa(PublicKey);
            var b1        = signature.Take(32).Reverse().Concat(new byte[] { 0x00 }).ToArray();
            var b2        = signature.Skip(32).Reverse().Concat(new byte[] { 0x00 }).ToArray();
            var num1      = new BigInteger(b1);
            var num2      = new BigInteger(b2);
            var hash      = sha256.ComputeHash(message);

            return(ecdsa.VerifySignature(hash, num1, num2));
            //var PublicKey = ThinNeo.Cryptography.ECC.ECPoint.DecodePoint(pubkey, ThinNeo.Cryptography.ECC.ECCurve.Secp256r1);
            //var usepk = PublicKey.EncodePoint(false).Skip(1).ToArray();

            ////byte[] first = { 0x45, 0x43, 0x53, 0x31, 0x20, 0x00, 0x00, 0x00 };
            ////usepk = first.Concat(usepk).ToArray();

            ////using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(usepk, System.Security.Cryptography.CngKeyBlobFormat.EccPublicBlob))
            ////using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))

            //using (var ecdsa = System.Security.Cryptography.ECDsa.Create(new System.Security.Cryptography.ECParameters
            //{
            //    Curve = System.Security.Cryptography.ECCurve.NamedCurves.nistP256,
            //    Q = new System.Security.Cryptography.ECPoint
            //    {
            //        X = usepk.Take(32).ToArray(),
            //        Y = usepk.Skip(32).ToArray()
            //    }
            //}))
            //{
            //    var hash = sha256.ComputeHash(message);
            //    return ecdsa.VerifyHash(hash, signature);
            //}
        }
Example #2
0
        public static byte[] Sign(byte[] message, byte[] prikey)
        {
            var Secp256r1_G = Helper.HexString2Bytes("04" + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");

            var PublicKey = ThinNeo.Cryptography.ECC.ECCurve.Secp256r1.G * prikey;
            var pubkey    = PublicKey.EncodePoint(false).Skip(1).ToArray();

            var ecdsa  = new ThinNeo.Cryptography.ECC.ECDsa(prikey, ThinNeo.Cryptography.ECC.ECCurve.Secp256r1);
            var hash   = sha256.ComputeHash(message);
            var result = ecdsa.GenerateSignature(hash);
            var data1  = result[0].ToByteArray();

            if (data1.Length > 32)
            {
                data1 = data1.Take(32).ToArray();
            }
            var data2 = result[1].ToByteArray();

            if (data2.Length > 32)
            {
                data2 = data2.Take(32).ToArray();
            }

            data1 = data1.Reverse().ToArray();
            data2 = data2.Reverse().ToArray();

            byte[] newdata = new byte[64];
            Array.Copy(data1, 0, newdata, 32 - data1.Length, data1.Length);
            Array.Copy(data2, 0, newdata, 64 - data2.Length, data2.Length);

            return(newdata);// data1.Concat(data2).ToArray();
        }
Example #3
0
        public static byte[] Sign(byte[] message, byte[] prikey)
        {
            var Secp256r1_G = Helper.HexString2Bytes("04" + "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296" + "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5");

            var PublicKey = ThinNeo.Cryptography.ECC.ECCurve.Secp256r1.G * prikey;
            var pubkey    = PublicKey.EncodePoint(false).Skip(1).ToArray();

            var ecdsa = new ThinNeo.Cryptography.ECC.ECDsa(prikey, ThinNeo.Cryptography.ECC.ECCurve.Secp256r1);

            System.Security.Cryptography.SHA256 sha256 = getSha256();
            var hash   = sha256.ComputeHash(message);
            var result = ecdsa.GenerateSignature(hash);
            var data1  = result[0].ToByteArray();

            if (data1.Length > 32)
            {
                data1 = data1.Take(32).ToArray();
            }
            var data2 = result[1].ToByteArray();

            if (data2.Length > 32)
            {
                data2 = data2.Take(32).ToArray();
            }

            data1 = data1.Reverse().ToArray();
            data2 = data2.Reverse().ToArray();

            byte[] newdata = new byte[64];
            Array.Copy(data1, 0, newdata, 32 - data1.Length, data1.Length);
            Array.Copy(data2, 0, newdata, 64 - data2.Length, data2.Length);

            return(newdata);// data1.Concat(data2).ToArray();
            //#if NET461
            //const int ECDSA_PRIVATE_P256_MAGIC = 0x32534345;
            //byte[] first = { 0x45, 0x43, 0x53, 0x32, 0x20, 0x00, 0x00, 0x00 };
            //prikey = first.Concat(pubkey).Concat(prikey).ToArray();
            //using (System.Security.Cryptography.CngKey key = System.Security.Cryptography.CngKey.Import(prikey, System.Security.Cryptography.CngKeyBlobFormat.EccPrivateBlob))
            //using (System.Security.Cryptography.ECDsaCng ecdsa = new System.Security.Cryptography.ECDsaCng(key))

            //using (var ecdsa = System.Security.Cryptography.ECDsa.Create(new System.Security.Cryptography.ECParameters
            //{
            //    Curve = System.Security.Cryptography.ECCurve.NamedCurves.nistP256,
            //    D = prikey,
            //    Q = new System.Security.Cryptography.ECPoint
            //    {
            //        X = pubkey.Take(32).ToArray(),
            //        Y = pubkey.Skip(32).ToArray()
            //    }
            //}))
            //{
            //    var hash = sha256.ComputeHash(message);
            //    return ecdsa.SignHash(hash);
            //}
        }
Example #4
0
        public static bool VerifySignature(byte[] message, byte[] signature, byte[] pubkey)
        {
            //unity dotnet不完整,不能用dotnet自带的ecdsa
            var PublicKey = ThinNeo.Cryptography.ECC.ECPoint.DecodePoint(pubkey, ThinNeo.Cryptography.ECC.ECCurve.Secp256r1);
            var ecdsa     = new ThinNeo.Cryptography.ECC.ECDsa(PublicKey);
            var b1        = signature.Take(32).Reverse().Concat(new byte[] { 0x00 }).ToArray();
            var b2        = signature.Skip(32).Reverse().Concat(new byte[] { 0x00 }).ToArray();
            var num1      = new BigInteger(b1);
            var num2      = new BigInteger(b2);
            var hash      = sha256.ComputeHash(message);

            return(ecdsa.VerifySignature(hash, num1, num2));
        }