Ejemplo n.º 1
0
        /// <summary>
        /// Validates the signature using the public part of the asymmetric key given as parameter.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <returns><code>true</code> if the signature is present and can be verified using the given key.
        /// <code>false</code> if the signature is present, but can't be verified using the given key.</returns>
        /// <exception cref="InvalidOperationException">If the query is not signed, and therefore cannot have its signature verified. Use
        /// the <code>IsSigned</code> property to check for this situation before calling this method.</exception>
        public bool CheckSignature(AsymmetricAlgorithm key)
        {
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }

            if (!(key is DSA || key is RSA))
            {
                throw new ArgumentException("The key must be an instance of either DSA or RSACryptoServiceProvider.");
            }

            if (!IsSigned)
            {
                throw new InvalidOperationException("Query is not signed, so there is no signature to verify.");
            }

            var hashAlgorithm = XmlSignatureUtils.GetHashAlgorithm(SignatureAlgorithm);
            var hash          = hashAlgorithm.ComputeHash(Encoding.UTF8.GetBytes(_signedquery));

            if (key is RSA)
            {
                var rsa = (RSA)key;
                return(VerifyHash(hashAlgorithm, rsa, hash, DecodeSignature()));
            }
            else
            {
                var dsa = (DSA)key;
                return(dsa.VerifySignature(hash, DecodeSignature()));
            }
        }