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)); }
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); } }
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); } }
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); } }
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; }