コード例 #1
0
        public static string GeneratePrivateKey()
        {
            var privateKey = new byte[32];
            var rnd        = System.Security.Cryptography.RandomNumberGenerator.Create();

            rnd.GetBytes(privateKey);
            return(Base58Encoding.EncodePrivateKey(privateKey));
        }
コード例 #2
0
        public static (string privateKey, string AccountId) GenerateWallet(byte[] keyData)
        {
            var pvtKeyStr = Base58Encoding.EncodePrivateKey(keyData);

            var pubKey = GetAccountIdFromPrivateKey(pvtKeyStr);

            return(pvtKeyStr, pubKey);
        }
コード例 #3
0
        public static (string privateKey, string AccountId) GenerateWallet(byte [] keyData)
        {
            var kp = new KeyPair(keyData);

            var pvtKeyStr = Base58Encoding.EncodePrivateKey(keyData);

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

            return(pvtKeyStr, Base58Encoding.EncodeAccountId(pubKey));
        }
コード例 #4
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));
        }
コード例 #5
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));
        }
コード例 #6
0
 public static bool ValidatePrivateKey(string PrivateKey)
 {
     try
     {
         Base58Encoding.DecodePrivateKey(PrivateKey);
         return(true);
     }
     catch
     {
         return(false);
     }
 }
コード例 #7
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));
            }
コード例 #8
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);
            }
コード例 #9
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(1, pkbytes);
            var    q       = domain.G.Multiply(d);

            var publicKey = new ECPublicKeyParameters(q, domain);

            return(publicKey.Q.GetEncoded(false));
        }
コード例 #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
            {
                //_log?.LogError("VerifySignature failed: " + ex.Message);
                return(false);
            }
        }
コード例 #11
0
        public static bool ValidateAccountId(string AccountId)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(AccountId) || AccountId[0] != LyraGlobal.ADDRESSPREFIX)
                {
                    return(false);
                }

                Base58Encoding.DecodeAccountId(AccountId);
                return(true);
            }
            catch
            {
                return(false);
            }
        }
コード例 #12
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 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));
        }
コード例 #13
0
        public byte[] GetSharedSecret(string LocalPrivateKey, string RemoteAccountId)
        {
            //var pvtKey = Base58Encoding.DecodePrivateKey(privateKey);
            //var kp = new Neo.Wallets.KeyPair(pvtKey);
            //return Base58Encoding.EncodeAccountId(kp.PublicKey.EncodePoint(false).Skip(1).ToArray());

            //var curve = SecNamedCurves.GetByName("secp256k1");
            var curve  = SecNamedCurves.GetByName("secp256r1");
            var domain = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);

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

            var privateKeyParameters = new ECPrivateKeyParameters(new BigInteger(1, pkbytes), domain);

            var dh = new ECDHCBasicAgreement();

            dh.Init(privateKeyParameters);


            //var publicKeyBytes = Base58Encoding.DecodeWithCheckSum(RemotePublicKey);
            var publicKeyBytes = Base58Encoding.DecodeAccountId(RemoteAccountId);



            //var q = curve.Curve.DecodePoint(publicKeyBytes);
            var q = curve.Curve.CreatePoint(new BigInteger(1, publicKeyBytes.Take(32).ToArray()), new BigInteger(1, publicKeyBytes.Skip(32).ToArray()));

            var publicKeyParameters = new ECPublicKeyParameters(q, domain);

            var sharedSecret = dh.CalculateAgreement(publicKeyParameters);

            using (var sha = SHA256.Create())
            {
                var hash = sha.ComputeHash(sharedSecret.ToByteArray());
                return(hash);
            }
        }
コード例 #14
0
        public static string GetSignature(string privateKey, string message, string AccountId)
        {
            if (IsMono)
            {
                return(PortableSignatures.GetSignature(privateKey, message));
            }

            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;
        }
コード例 #15
0
 public static string GetPublicKeyFromPrivateKey(string privateKey)
 {
     byte[] public_key_bytes = DerivePublicKeyBytes(privateKey);
     return(Base58Encoding.EncodePublicKey(public_key_bytes));
 }
コード例 #16
0
 public static string GetAccountIdFromPrivateKey(string privateKey)
 {
     byte[] public_key_bytes = DerivePublicKeyBytes(privateKey);
     return(Base58Encoding.EncodeAccountId(public_key_bytes.Skip(1).ToArray()));   // skip first byte which indicate compress or not.
 }