コード例 #1
0
        /// <summary>
        /// Generates a signature for the specified data.
        /// </summary>
        /// <param name="data">The message data to be signed.</param>
        /// <param name="offset">The offset index in the message data to start signing.</param>
        /// <param name="count">The number of bytes from the offset to sign.</param>
        /// <returns>A digital signature for the specified data.</returns>
        public byte[] SignData(byte[] data, int offset, int count)
        {
            if (data == null)
            {
                throw new ArgumentNullException();
            }
            if (offset < 0 || offset > data.Length)
            {
                throw new ArgumentOutOfRangeException();
            }
            if (count < 0 || count > data.Length - offset)
            {
                throw new ArgumentOutOfRangeException();
            }

            byte[] retVal;

            m_signMech.Parameter = Utility.ConvertToBytes((int)m_hashAlgorithm);

            using (CryptokiSign sig = new CryptokiSign(m_session, m_signMech, KeyPair))
            {
                retVal = sig.Sign(data, offset, count);
            }

            return(retVal);
        }
コード例 #2
0
        /// <summary>
        /// Computes the hash value of the specified input stream using the specified hash algorithm, and signs the resulting hash value.
        /// </summary>
        /// <param name="inputStream">The input data for which to compute the hash.</param>
        /// <param name="hash">The hash algorithm to use to create the hash value.</param>
        /// <returns>The RSA signature for the specified data.</returns>
        public byte[] SignData(Stream inputStream, HashAlgorithm hash)
        {
            byte[] retVal;
            byte[] data;

            if (hash != null)
            {
                m_signHashMech.Parameter = Utility.ConvertToBytes((int)hash.HashType);
            }
            else
            {
                m_signHashMech.Parameter = Utility.ConvertToBytes((int)m_hashMech);
            }

            // TODO: look at breaking this up for stream so that it i only reads x amount at a time
            data = new byte[(int)inputStream.Length];

            int len = inputStream.Read(data, 0, data.Length);

            using (CryptokiSign sig = new CryptokiSign(m_session, m_signHashMech, KeyPair))
            {
                retVal = sig.Sign(data, 0, len);
            }

            return(retVal);
        }
コード例 #3
0
        /// <summary>
        /// Generates a signature for the specified hash value.
        /// </summary>
        /// <param name="hash">The hash value of the data to be signed.</param>
        /// <param name="hashAlgorithm">The hash algorithm used to create the hash value of the data.</param>
        /// <returns>A digital signature for the specified hash value.</returns>
        public byte[] SignHash(byte[] hash, MechanismType hashAlgorithm)
        {
            if (hash == null)
            {
                throw new ArgumentNullException();
            }

            byte[] sig = null;

            m_signMech.Parameter = Utility.ConvertToBytes((int)(hashAlgorithm | MechanismType.SIGN_NO_NODIGEST_FLAG));

            using (CryptokiSign sign = new CryptokiSign(m_session, m_signMech, KeyPair))
            {
                sig = sign.Sign(hash, 0, hash.Length);
            }

            return(sig);
        }
コード例 #4
0
        /// <summary>
        /// Computes the hash value of a subset of the specified byte array using the specified hash algorithm, and signs the resulting hash value.
        /// </summary>
        /// <param name="buffer">The input data for which to compute the hash.</param>
        /// <param name="offset">The offset into the array from which to begin using data.</param>
        /// <param name="count">The number of bytes in the array to use as data.</param>
        /// <param name="hash">The hash algorithm to use to create the hash value.</param>
        /// <returns>The RSA signature for the specified data.</returns>
        public byte[] SignData(byte[] buffer, int offset, int count, HashAlgorithm hash)
        {
            byte[] retVal;

            if (hash != null)
            {
                //return SignHash(hash.ComputeHash(buffer, offset, count));
                m_signHashMech.Parameter = Utility.ConvertToBytes((int)hash.HashType);
            }
            else
            {
                m_signHashMech.Parameter = Utility.ConvertToBytes((int)m_hashMech);
            }

            using (CryptokiSign sig = new CryptokiSign(m_session, m_signHashMech, KeyPair))
            {
                retVal = sig.Sign(buffer, offset, count);
            }

            return(retVal);
        }