Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the System.Security.Cryptography.PBKDF2 class using a password, a generated salt of the specified size, a number of iterations, and the name of a System.Security.Cryptography.HMAC hashing implementation to derive the key.
        /// </summary>
        /// <param name="password">The password to derive the key for.</param>
        /// <param name="saltSize">The salt to use to derive the key.</param>
        /// <param name="iterations">The number of iterations to use to derive the key.</param>
        /// <param name="hashName">The name of the System.Security.Cryptography.HMAC implementation to use to derive the key.</param>
        /// <exception cref="System.ArgumentNullException">The password or algorithm is null.</exception>
        /// <exception cref="System.ArgumentException">The salt size is less than 8 or the iterations is less than 1.</exception>
        public PBKDF2(string password, int saltSize, int iterations, string hashName)
        {
            if (string.IsNullOrWhiteSpace(password))
            {
                throw new ArgumentNullException("password");
            }
            if (string.IsNullOrWhiteSpace(hashName))
            {
                throw new ArgumentNullException("hashName");
            }
            if (saltSize < 8)
            {
                throw new ArgumentOutOfRangeException("saltSize", "Argument cannot be less than 8.");
            }
            if (iterations < 1)
            {
                throw new ArgumentException("Argument must be greater than zero.", "iterations");
            }

            _password       = new UTF8Encoding(false).GetBytes(password);
            _salt           = Utils.GenerateSalt(saltSize);
            _hashName       = hashName;
            _iterationCount = (uint)iterations;
            Initialize();
        }