/// <summary>
 /// Creates secure password based on the predefined input.
 /// Hash algorithm must implement IHash interface and choosen algorithm
 /// will determine the size of password returned.
 /// </summary>
 /// <param name="input">Input as string. It's called predefined input.</param>
 /// <param name="hashCrypto">Concrete hash algorithm that implements IHash interface.</param>
 /// <param name="iteration">Iteration count.</param>
 /// <returns>Random password in bytes.</returns>
 /// <exception cref="ArgumentNullException">'input' cannot be null/empty.</exception>
 /// <exception cref="ArgumentNullException">'hashCrypto' cannot be null.</exception>
 /// <exception cref="ArgumentOutOfRangeException">'iteration' is invalid.</exception>
 public static byte[] CreateSecurePassword(string input, IHashCrypto hashCrypto, int iteration = 10000)
 {
     if (String.IsNullOrEmpty(input))
     {
         throw new ArgumentNullException(nameof(input));
     }
     if (hashCrypto == null)
     {
         throw new ArgumentNullException(nameof(hashCrypto));
     }
     if (iteration < 5000)
     {
         throw new ArgumentOutOfRangeException(nameof(iteration), "Min value for iteration is 5000.");
     }
     byte[] data = null;
     try
     {
         byte[] salt = hashCrypto.GetHashBytes(input);
         using (Rfc2898DeriveBytes generator = new Rfc2898DeriveBytes(input, salt, iteration))
         {
             data = generator.GetBytes(hashCrypto.HashSize / 8);
         }
     }
     catch { data = null; }
     return(data);
 }
Exemplo n.º 2
0
 /// <summary>
 /// Injectable constructor. Used commonly for dependency injection.
 /// </summary>
 /// <param name="hash">Concreate implementation of IHash interface.</param>
 public HashCrypto(IHashCrypto hash)
 {
     _hash = hash;
     if (_hash == null)
     {
         _hash = new SHA256Crypto();
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Compares 2 data using algorithm that implements IHashCrypto interface.
        /// </summary>
        /// <param name="data1">Data 1.</param>
        /// <param name="data2">Data 2.</param>
        /// <param name="hash">Hash algorithm that implements IHashCrypto interface.</param>
        /// <returns>Returs true if both are same, otherwise false.</returns>
        /// <exception cref="ArgumentNullException">First data cannot be null.</exception>
        /// <exception cref="ArgumentNullException">Second data cannot be null.</exception>
        /// <exception cref="ArgumentNullException">Hash algorithm cannot be null.</exception>
        public static bool CompareHashes(byte[] data1, byte[] data2, IHashCrypto hash)
        {
            if (data1 == null)
            {
                throw new ArgumentNullException(nameof(data1), "First data cannot be null.");
            }
            if (data2 == null)
            {
                throw new ArgumentNullException(nameof(data2), "Second data cannot be null.");
            }
            if (hash == null)
            {
                throw new ArgumentNullException(nameof(hash), "Hash algorithm cannot be null.");
            }
            if (data1.Length != data2.Length)
            {
                return(false);
            }
            byte[] hashedData1 = null;
            byte[] hashedData2 = null;
            try
            {
                hashedData1 = hash.GetHashBytes(data1);
            }
            catch { hashedData1 = hashedData1 != null ? null : hashedData1; }
            try
            {
                hashedData2 = hash.GetHashBytes(data2);
            }
            catch { hashedData2 = hashedData2 != null ? null : hashedData2; }

            if (hashedData1 == null || hashedData2 == null)
            {
                return(false);
            }
            for (int i = 0; i < hashedData1.Length; i++)
            {
                if (hashedData1[i] != hashedData2[i])
                {
                    return(false);
                }
            }
            return(true);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Main constructor. If no parameter passed, SHA256Crypto will be used.
 /// </summary>
 public HashCrypto()
 {
     _hash = new SHA256Crypto();
 }