Пример #1
0
        static void Main(string[] args)
        {
            // Create a secp256k1 context (ensure disposal to prevent unmanaged memory leaks).
            using (var secp256k1 = new Secp256k1())
            {
                // Generate a private key.
                var privateKey = new byte[32];
                var rnd        = System.Security.Cryptography.RandomNumberGenerator.Create();
                do
                {
                    rnd.GetBytes(privateKey);
                }while (!secp256k1.SecretKeyVerify(privateKey));


                // Create public key from private key.
                var publicKey = new byte[64];
                Debug.Assert(secp256k1.PublicKeyCreate(publicKey, privateKey));


                // Sign a message hash.
                var messageBytes = Encoding.UTF8.GetBytes("Hello world.");
                var messageHash  = System.Security.Cryptography.SHA256.Create().ComputeHash(messageBytes);
                var signature    = new byte[64];
                Debug.Assert(secp256k1.Sign(signature, messageHash, privateKey));

                // Serialize a DER signature from ECDSA signature
                byte[] signatureDer = new byte[Secp256k1.SERIALIZED_DER_SIGNATURE_MAX_SIZE];
                int    outL         = 0;
                Debug.Assert(secp256k1.SignatureSerializeDer(signatureDer, signature, out outL));
                Array.Resize(ref signatureDer, outL);

                // Verify message hash.
                Debug.Assert(secp256k1.Verify(signature, messageHash, publicKey));
            }
        }
Пример #2
0
        public byte[] Sign(HashDigest <T> messageHash, PrivateKey privateKey)
        {
            lock (_instanceLock)
            {
                var secp256K1Signature = new byte[Secp256k1.SIGNATURE_LENGTH];
                var privateKeyBytes    = new byte[Secp256k1.PRIVKEY_LENGTH];

                privateKey.ByteArray.CopyTo(
                    privateKeyBytes,
                    Secp256k1.PRIVKEY_LENGTH - privateKey.ByteArray.Length);

                _instance.Sign(
                    secp256K1Signature,
                    messageHash.ToByteArray(),
                    privateKeyBytes);

                var signature = new byte[Secp256k1.SERIALIZED_DER_SIGNATURE_MAX_SIZE];
                _instance.SignatureSerializeDer(
                    signature,
                    secp256K1Signature,
                    out int signatureLength);

                return(signature.Take(signatureLength).ToArray());
            }
        }
Пример #3
0
        //Ignore this function as this is just to create Public Private key pairs for testing
        public static void GenerateKeys()
        {
            using (var secp256k1 = new Secp256k1())
            {
                // privatekey= "105c301c92f5d956ad577105e71aba4d29cf7af04cd47c648244dd8ad677381f"
                // publickey = "7a89dec4cc7e0964ed4c5e517f1cfee7e4f145e8500f55fe0317f97e71b7ba5219a4215b1885ac547da87bd0155d02c9bbe0501d0670a4f481df2b42f2130c02"

                // Generate a private key.
                var privateKey = new byte[32];
                var rnd        = System.Security.Cryptography.RandomNumberGenerator.Create();
                do
                {
                    rnd.GetBytes(privateKey);
                }while (!secp256k1.SecretKeyVerify(privateKey));

                var prk = ToHex(privateKey);

                // Create public key from private key.
                var publicKey = new byte[64];
                Debug.Assert(secp256k1.PublicKeyCreate(publicKey, privateKey));

                var pbk = ToHex(publicKey);


                // Sign a message hash.
                var messageBytes = Encoding.UTF8.GetBytes("Hello world.");
                var messageHash  = System.Security.Cryptography.SHA256.Create().ComputeHash(messageBytes);
                var signature    = new byte[64];
                Debug.Assert(secp256k1.Sign(signature, messageHash, privateKey));

                // Serialize a DER signature from ECDSA signature
                byte[] signatureDer = new byte[Secp256k1.SERIALIZED_DER_SIGNATURE_MAX_SIZE];
                int    outL         = 0;
                Debug.Assert(secp256k1.SignatureSerializeDer(signatureDer, signature, out outL));
                Array.Resize(ref signatureDer, outL);

                // Verify message hash.
                Debug.Assert(secp256k1.Verify(signature, messageHash, publicKey));
            }
        }