Beispiel #1
0
        /// <summary>
        /// Calls EVP_SignFinal()
        /// </summary>
        /// <param name="md"></param>
        /// <param name="bio"></param>
        /// <param name="pkey"></param>
        /// <returns></returns>
        public static byte[] Sign(MessageDigest md, BIO bio, CryptoKey pkey)
        {
            BIO bmd = BIO.MessageDigest(md);

            bmd.Push(bio);

            while (true)
            {
                ArraySegment <byte> bytes = bmd.ReadBytes(1024 * 4);
                if (bytes.Count == 0)
                {
                    break;
                }
            }

            MessageDigestContext ctx = new MessageDigestContext(bmd);

            byte[] sig = new byte[pkey.Size];
            uint   len = (uint)sig.Length;

            Native.ExpectSuccess(Native.EVP_SignFinal(ctx.Handle, sig, ref len, pkey.Handle));
            byte[] ret = new byte[len];
            Buffer.BlockCopy(sig, 0, ret, 0, (int)len);
            return(ret);
        }
Beispiel #2
0
        /// <summary>
        /// Calls EVP_VerifyFinal()
        /// </summary>
        /// <param name="md"></param>
        /// <param name="bio"></param>
        /// <param name="sig"></param>
        /// <param name="pkey"></param>
        /// <returns></returns>
        public static bool Verify(MessageDigest md, BIO bio, byte[] sig, CryptoKey pkey)
        {
            BIO bmd = BIO.MessageDigest(md);

            bmd.Push(bio);

            while (true)
            {
                ArraySegment <byte> bytes = bmd.ReadBytes(1024 * 4);
                if (bytes.Count == 0)
                {
                    break;
                }
            }

            MessageDigestContext ctx = new MessageDigestContext(bmd);

            int ret = Native.ExpectSuccess(Native.EVP_VerifyFinal(ctx.Handle, sig, (uint)sig.Length, pkey.Handle));

            return(ret == 1);
        }