示例#1
0
        public string SignString(string contents, string privateKey)
        {
            var curve         = SecNamedCurves.GetByName("secp256k1");
            var domain        = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
            var keyParameters = new ECPrivateKeyParameters(new BigInteger(privateKey, 16), domain);

            Org.BouncyCastle.Crypto.ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");
            signer.Init(true, keyParameters);
            signer.BlockUpdate(Encoding.ASCII.GetBytes(contents), 0, contents.Length);
            var signature = signer.GenerateSignature();

            return(Base58Encoding.Encode(signature));
        }
示例#2
0
        public string GeneratePublicKey(string privateKey)
        {
            if (String.IsNullOrWhiteSpace(privateKey))
            {
                throw new FormatException("Empty private key");
            }

            var curve     = SecNamedCurves.GetByName("secp256k1");
            var domain    = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
            var d         = new BigInteger(privateKey, 16);
            var q         = domain.G.Multiply(d);
            var publicKey = new ECPublicKeyParameters(q, domain);

            return(Base58Encoding.Encode(publicKey.Q.GetEncoded()));
        }
示例#3
0
        public bool SignatureIsValid(string signature, string contents, string publicKey)
        {
            var curve          = SecNamedCurves.GetByName("secp256k1");
            var domain         = new ECDomainParameters(curve.Curve, curve.G, curve.N, curve.H);
            var publicKeyBytes = Base58Encoding.Decode(publicKey);
            var q             = curve.Curve.DecodePoint(publicKeyBytes);
            var keyParameters = new ECPublicKeyParameters(q, domain);

            Org.BouncyCastle.Crypto.ISigner signer = SignerUtilities.GetSigner("SHA-256withECDSA");
            signer.Init(false, keyParameters);
            signer.BlockUpdate(Encoding.ASCII.GetBytes(contents), 0, contents.Length);
            var signatureBytes = Base58Encoding.Decode(signature);

            return(signer.VerifySignature(signatureBytes));
        }