Exemple #1
0
 internal static string CreateHash(IHashProvider provider, string plaintext)
 {
     byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);
     byte[] resultBytes    = provider.CreateHash(plainTextBytes);
     CryptographyUtility.GetRandomBytes(plainTextBytes);
     return(Convert.ToBase64String(resultBytes));
 }
Exemple #2
0
        internal static string EncryptSymmetric(string symmetricInstance, string plaintext, ConfigurationContext context)
        {
            ArgumentValidation.CheckForNullReference(symmetricInstance, "symmetricInstance");
            ArgumentValidation.CheckForEmptyString(symmetricInstance, "symmetricInstance");

            byte[] plainTextBytes  = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] cipherTextBytes = EncryptSymmetric(symmetricInstance, plainTextBytes, context);
            CryptographyUtility.GetRandomBytes(plainTextBytes);
            return(Convert.ToBase64String(cipherTextBytes));
        }
Exemple #3
0
        internal static bool CompareHash(IHashProvider provider, string plaintext, string hashedText)
        {
            byte[] plainTextBytes  = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] hashedTextBytes = Convert.FromBase64String(hashedText);

            bool result = provider.CompareHash(plainTextBytes, hashedTextBytes);

            CryptographyUtility.GetRandomBytes(plainTextBytes);

            return(result);
        }
Exemple #4
0
        internal static string CreateHash(string hashInstance, string plaintext, ConfigurationContext context)
        {
            ArgumentValidation.CheckForNullReference(hashInstance, "hashInstance");
            ArgumentValidation.CheckForEmptyString(hashInstance, "hashInstance");

            byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] resultBytes    = CreateHash(hashInstance, plainTextBytes, context);
            CryptographyUtility.GetRandomBytes(plainTextBytes);

            return(Convert.ToBase64String(resultBytes));
        }
Exemple #5
0
        /// <summary>
        /// Encrypts a secret using a specified symmetric cryptography provider.
        /// </summary>
        /// <param name="symmetricInstance">A symmetric instance from configuration.</param>
        /// <param name="plaintext">The input as a base64 encoded string for which you want to encrypt.</param>
        /// <returns>The resulting cipher text as a base64 encoded string.</returns>
        public static string EncryptSymmetric(string symmetricInstance, string plaintext)
        {
            if (string.IsNullOrEmpty(symmetricInstance))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "symmetricInstance");
            }

            byte[] plainTextBytes  = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] cipherTextBytes = EncryptSymmetric(symmetricInstance, plainTextBytes);
            CryptographyUtility.GetRandomBytes(plainTextBytes);
            return(Convert.ToBase64String(cipherTextBytes));
        }
Exemple #6
0
        internal static string EncryptSymmetric(ISymmetricCryptoProvider provider, string plaintext)
        {
            if (string.IsNullOrEmpty(plaintext))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "plaintext");
            }

            byte[] plainTextBytes  = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] cipherTextBytes = provider.Encrypt(plainTextBytes);
            CryptographyUtility.GetRandomBytes(plainTextBytes);
            return(Convert.ToBase64String(cipherTextBytes));
        }
Exemple #7
0
        /// <summary>
        /// Computes the hash value of plain text using the given hash provider instance
        /// </summary>
        /// <param name="hashInstance">A hash instance from configuration.</param>
        /// <param name="plaintext">The input for which to compute the hash.</param>
        /// <returns>The computed hash code.</returns>
        public static string CreateHash(string hashInstance, string plaintext)
        {
            if (string.IsNullOrEmpty(hashInstance))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "hashInstance");
            }

            byte[] plainTextBytes = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] resultBytes    = CreateHash(hashInstance, plainTextBytes);
            CryptographyUtility.GetRandomBytes(plainTextBytes);

            return(Convert.ToBase64String(resultBytes));
        }
Exemple #8
0
        internal static string DecryptSymmetric(string symmetricInstance, string ciphertextBase64, ConfigurationContext context)
        {
            ArgumentValidation.CheckForNullReference(symmetricInstance, "symmetricInstance");
            ArgumentValidation.CheckForEmptyString(symmetricInstance, "symmetricInstance");

            byte[] cipherTextBytes = Convert.FromBase64String(ciphertextBase64);
            byte[] decryptedBytes  = DecryptSymmetric(symmetricInstance, cipherTextBytes, context);
            string decryptedString = UnicodeEncoding.Unicode.GetString(decryptedBytes);

            CryptographyUtility.GetRandomBytes(decryptedBytes);

            return(decryptedString);
        }
        private void AddSaltToPlainText(ref byte[] salt, ref byte[] plaintext)
        {
            if (!saltEnabled)
            {
                return;
            }

            if (salt == null)
            {
                salt = CryptographyUtility.GetRandomBytes(SaltLength);
            }

            plaintext = CryptographyUtility.CombineBytes(salt, plaintext);
        }
Exemple #10
0
        internal static bool CompareHash(string hashInstance, string plaintext, string hashedText, ConfigurationContext context)
        {
            ArgumentValidation.CheckForNullReference(hashInstance, "hashInstance");
            ArgumentValidation.CheckForEmptyString(hashInstance, "hashInstance");

            byte[] plainTextBytes  = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] hashedTextBytes = Convert.FromBase64String(hashedText);

            bool result = CompareHash(hashInstance, plainTextBytes, hashedTextBytes, context);

            CryptographyUtility.GetRandomBytes(plainTextBytes);

            return(result);
        }
Exemple #11
0
        internal static string DecryptSymmetric(ISymmetricCryptoProvider provider, string ciphertextBase64)
        {
            if (string.IsNullOrEmpty(ciphertextBase64))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "ciphertextBase64");
            }

            byte[] cipherTextBytes = Convert.FromBase64String(ciphertextBase64);
            byte[] decryptedBytes  = provider.Decrypt(cipherTextBytes);
            string decryptedString = UnicodeEncoding.Unicode.GetString(decryptedBytes);

            CryptographyUtility.GetRandomBytes(decryptedBytes);

            return(decryptedString);
        }
Exemple #12
0
        /// <summary>
        /// Compares plain text input with a computed hash using the given hash provider instance.
        /// </summary>
        /// <remarks>
        /// Use this method to compare hash values. Since hashes contain a random "salt" value, two seperately generated
        /// hashes of the same plain text will result in different values.
        /// </remarks>
        /// <param name="hashInstance">A hash instance from configuration.</param>
        /// <param name="plaintext">The input as a string for which you want to compare the hash to.</param>
        /// <param name="hashedText">The hash as a string 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 static bool CompareHash(string hashInstance, string plaintext, string hashedText)
        {
            if (string.IsNullOrEmpty(hashInstance))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "hashInstance");
            }

            byte[] plainTextBytes  = UnicodeEncoding.Unicode.GetBytes(plaintext);
            byte[] hashedTextBytes = Convert.FromBase64String(hashedText);

            bool result = CompareHash(hashInstance, plainTextBytes, hashedTextBytes);

            CryptographyUtility.GetRandomBytes(plainTextBytes);

            return(result);
        }
Exemple #13
0
        private void AddSaltToPlainText(ref byte[] salt, ref byte[] plaintext)
        {
            HashAlgorithmProviderData data = GetHashAlgorithmProviderDataFromCursor();

            if (!data.SaltEnabled)
            {
                return;
            }

            if (salt == null)
            {
                salt = CryptographyUtility.GetRandomBytes(SaltLength);
            }

            plaintext = CryptographyUtility.CombineBytes(salt, plaintext);
        }
Exemple #14
0
        /// <summary>
        /// Decrypts a cipher text using a specified symmetric cryptography provider.
        /// </summary>
        /// <param name="symmetricInstance">A symmetric instance from configuration.</param>
        /// <param name="ciphertextBase64">The cipher text as a base64 encoded string for which you want to decrypt.</param>
        /// <returns>The resulting plain text as a string.</returns>
        public static string DecryptSymmetric(string symmetricInstance, string ciphertextBase64)
        {
            if (string.IsNullOrEmpty(symmetricInstance))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "symmetricInstance");
            }
            if (string.IsNullOrEmpty(ciphertextBase64))
            {
                throw new ArgumentException(Resources.ExceptionNullOrEmptyString, "ciphertextBase64");
            }

            byte[] cipherTextBytes = Convert.FromBase64String(ciphertextBase64);
            byte[] decryptedBytes  = DecryptSymmetric(symmetricInstance, cipherTextBytes);
            string decryptedString = UnicodeEncoding.Unicode.GetString(decryptedBytes);

            CryptographyUtility.GetRandomBytes(decryptedBytes);

            return(decryptedString);
        }
Exemple #15
0
 private byte[] GenerateSalt()
 {
     return(CryptographyUtility.GetRandomBytes(16));
 }