ComputeSaltedPassword() public static method

Implements a PBKDF2 algorthim with a user definded MAC method.
public static ComputeSaltedPassword ( HMACMethod method, byte password, byte salt, int iterations, int length ) : byte[]
method HMACMethod the HMAC method to use.
password byte the password to use
salt byte the salt. Must be at least 64-bit
iterations int the number of iterations. Must be at least 1000
length int the number of bytes to return
return byte[]
Exemplo n.º 1
0
        public void TestPBE2()
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

            byte[] data = new byte[30];
            rng.GetBytes(data);
            _ = PBKDF2.ComputeSaltedPassword(HMACMethod.SHA512, Encoding.UTF8.GetBytes("test"), data, 400000, 20);
            sw.Stop();
            System.Console.WriteLine(sw.Elapsed.TotalMilliseconds);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Updates the <see cref="SaltedPassword"/>. Returns False if the password value did not change.
        /// </summary>
        /// <param name="hashMethod"></param>
        /// <param name="salt"></param>
        /// <param name="iterations"></param>
        /// <returns>Returns False if the password value did not change.</returns>
        public bool TryUpdate(HashMethod hashMethod, byte[] salt, int iterations)
        {
            bool hasChanged = false;

            if (m_salt == null || !salt.SecureEquals(m_salt))
            {
                hasChanged = true;
                m_salt     = salt;
            }

            if (m_hashMethod != hashMethod)
            {
                hasChanged   = true;
                m_hashMethod = hashMethod;
            }

            if (iterations != m_iterations)
            {
                hasChanged   = true;
                m_iterations = iterations;
            }

            if (hasChanged)
            {
                switch (hashMethod)
                {
                case HashMethod.Sha1:
                    SaltedPassword = PBKDF2.ComputeSaltedPassword(HMACMethod.SHA1, m_passwordBytes, m_salt, m_iterations, 20);
                    break;

                case HashMethod.Sha256:
                    SaltedPassword = PBKDF2.ComputeSaltedPassword(HMACMethod.SHA256, m_passwordBytes, m_salt, m_iterations, 32);
                    break;

                case HashMethod.Sha384:
                    SaltedPassword = PBKDF2.ComputeSaltedPassword(HMACMethod.SHA384, m_passwordBytes, m_salt, m_iterations, 48);
                    break;

                case HashMethod.Sha512:
                    SaltedPassword = PBKDF2.ComputeSaltedPassword(HMACMethod.SHA512, m_passwordBytes, m_salt, m_iterations, 64);
                    break;

                default:
                    throw new Exception("Invalid Hash Method");
                }
                return(true);
            }
            return(false);
        }