Пример #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);
        }
Пример #2
0
        /// <summary>
        /// <para>Decrypt the given data.</para>
        /// </summary>
        /// <param name="cipherText"><para>The cipher text that will be decrypted.</para></param>
        /// <param name="entropy"><para>The entropy that was used to salt the phrase.</para></param>
        /// <returns><para>The resulting plain text.</para></returns>
        public byte[] Decrypt(byte[] cipherText, byte[] entropy)
        {
            ArgumentValidation.CheckForNullReference(cipherText, "cipherText");
            ArgumentValidation.CheckForZeroBytes(cipherText, "cipherText");

            ValidateStoreEntropyArguments(entropy);
            return(DecryptInternal(cipherText, entropy));
        }
Пример #3
0
        /// <summary>
        /// <para>
        /// Encrypt given data; this overload should be used when storage mode is "Machine", since when storage mode
        /// is "Machine" you must define entropy to "salt" the phrase.
        /// </para>
        /// </summary>
        /// <param name="plaintext"><para>The plain text that will be encrypted.</para></param>
        /// <param name="entropy"><para>The entropy to salt the phrase.</para></param>
        /// <returns><para>The resulting cipher text.</para></returns>
        public byte[] Encrypt(byte[] plaintext, byte[] entropy)
        {
            ArgumentValidation.CheckForNullReference(plaintext, "plainText");
            ArgumentValidation.CheckForZeroBytes(plaintext, "plainText");

            ValidateStoreEntropyArguments(entropy);
            return(EncryptInternal(plaintext, entropy));
        }
Пример #4
0
        /// <summary>
        /// <para>Returns a string from a byte array represented as a hexidecimal number (eg: 0F351A).</para>
        /// </summary>
        /// <param name="bytes">
        /// <para>The byte array to convert to forat as a hexidecimal number.</para>
        /// </param>
        /// <returns>
        /// <para>The formatted representation of the bytes as a hexidcimal number.</para>
        /// </returns>
        public static string GetHexStringFromBytes(byte[] bytes)
        {
            ArgumentValidation.CheckForNullReference(bytes, "bytes");
            ArgumentValidation.CheckForZeroBytes(bytes, "bytes");

            StringBuilder sb = new StringBuilder(bytes.Length * 2);

            for (int i = 0; i < bytes.Length; i++)
            {
                sb.Append(bytes[i].ToString("X2", CultureInfo.InvariantCulture));
            }
            return(sb.ToString());
        }
Пример #5
0
        /// <summary>
        /// Decrypts a secret using the configured <c>SymmetricAlgorithm</c>.
        /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/>
        /// </summary>
        /// <param name="ciphertext"><para>The cipher text for which you want to decrypt.</para></param>
        /// <returns><para>The resulting plain text.</para></returns>
        /// <seealso cref="ISymmetricCryptoProvider.Decrypt"/>
        public byte[] Decrypt(byte[] ciphertext)
        {
            ArgumentValidation.CheckForNullReference(ciphertext, "encryptedText");
            ArgumentValidation.CheckForZeroBytes(ciphertext, "encryptedText");

            byte[] output = null;

            SymmetricAlgorithmProviderData data = GetSymmetricAlgorithmProviderDataFromCursor();

            SymmetricCryptographer crypto = new SymmetricCryptographer(data.AlgorithmType, data.Key);

            output = crypto.Decrypt(ciphertext);
            SecurityCryptoSymmetricDecryptionEvent.Fire(string.Empty);
            return(output);
        }