Ejemplo n.º 1
0
        /// <summary>
        /// Compares plain text input with a computed hash using the configured <c>HashAlgorithm</c>.
        /// <seealso cref="IHashProvider.CompareHash"/>
        /// </summary>
        /// <param name="plaintext"><seealso cref="IHashProvider.CompareHash"/></param>
        /// <param name="hashedtext"><seealso cref="IHashProvider.CompareHash"/></param>
        /// <returns><seealso cref="IHashProvider.CompareHash"/></returns>
        public bool CompareHash(byte[] plaintext, byte[] hashedtext)
        {
            ArgumentValidation.CheckForNullReference(plaintext, "plainText");
            ArgumentValidation.CheckForNullReference(hashedtext, "hashedText");
            ArgumentValidation.CheckForZeroBytes(hashedtext, "hashedText");

            bool result = false;

            byte[] hashedPlainText = null;
            byte[] salt            = null;
            try
            {
                salt            = ExtractSalt(hashedtext);
                hashedPlainText = CreateHashWithSalt(plaintext, salt);
            }
            finally
            {
                CryptographyUtility.ZeroOutBytes(salt);
            }
            result = CryptographyUtility.CompareBytes(hashedPlainText, hashedtext);
            SecurityCryptoHashCheckEvent.Fire(string.Empty);
            if (!result)
            {
                SecurityCryptoHashCheckFailureEvent.Fire(string.Empty);
            }
            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Compares plain text input with a computed hash.
        /// </summary>
        /// <param name="plaintext">The input for which you want to compare the hash to.</param>
        /// <param name="hashedtext">The hash value for which you want to compare the input to.</param>
        /// <returns><c>true</c> if plainText hashed is equal to the hashedText. Otherwise, <c>false</c>.</returns>
        public bool CompareHash(byte[] plaintext, byte[] hashedtext)
        {
            if (plaintext == null)
            {
                throw new ArgumentNullException("plainText");
            }
            if (hashedtext == null)
            {
                throw new ArgumentNullException("hashedText");
            }
            if (hashedtext.Length == 0)
            {
                throw new ArgumentException(Resources.ExceptionByteArrayValueMustBeGreaterThanZeroBytes, "hashedText");
            }

            bool result = false;

            byte[] hashedPlainText = null;
            byte[] salt            = null;
            try
            {
                try
                {
                    salt            = ExtractSalt(hashedtext);
                    hashedPlainText = CreateHashWithSalt(plaintext, salt);
                }
                finally
                {
                    CryptographyUtility.ZeroOutBytes(salt);
                }
                result = CryptographyUtility.CompareBytes(hashedPlainText, hashedtext);
            }
            catch (Exception e)
            {
                InstrumentationProvider.FireCyptographicOperationFailed(Resources.HashComparisonFailed, e);
                throw;
            }

            InstrumentationProvider.FireHashComparisonPerformed();
            if (!result)
            {
                InstrumentationProvider.FireHashMismatchDetected();
            }
            return(result);
        }