Example #1
0
        /// <summary>
        /// Password-Based Key Derivation Function 2
        /// https://github.com/defuse/password-hashing
        /// </summary>
        public override void Execute()
        {
            Verified = false;

            if (Salt != null && !string.IsNullOrEmpty(HashAlgorithm))
            {
                var passwordHash = GetHash(
                    Login,
                    Password,
                    Salt,
                    HashAlgorithm,
                    Iterations,
                    PasswordHashLength);

                Hash = string.Join(DELIMITER.ToString(), new string[]
                {
                    HashAlgorithm,
                    Iterations.ToString(),
                    PasswordHashLength.ToString(),
                    Convert.ToBase64String(Salt),
                    Convert.ToBase64String(passwordHash)
                });
            }
            else
            {
                Hash = null;
            }

            if (!string.IsNullOrEmpty(ToVerify) /*&& !string.IsNullOrEmpty(Login)*/ && !string.IsNullOrEmpty(Password))
            {
                var split = ToVerify.Split(DELIMITER);
                var hash  = Convert.FromBase64String(split[PASSWORD_HASH_INDEX]);

                var storedHashSize = Int32.Parse(split[PASSWORD_HASH_BYTES_INDEX]);
                if (storedHashSize == hash.Length)
                {
                    var computedHash = GetHash(
                        Login,
                        Password,
                        Convert.FromBase64String(split[SALT_INDEX]),
                        split[HASH_ALGORITHM_INDEX],
                        Int32.Parse(split[PBKDF2_ITERATIONS_INDEX]),
                        hash.Length);

                    Verified = SlowEquals(hash, computedHash);
                }
            }
            else
            {
                Verified = false;
            }
        }
Example #2
0
 public override string[] GetInputs()
 {
     return(new string[]
     {
         $"{ nameof(Login) }:{ Login }",
         $"{ nameof(Password) }:{ Password }",
         $"{ nameof(Iterations) }:{ Iterations.ToString() }",
         $"{ nameof(PasswordHashLength) }:{ PasswordHashLength.ToString() }",
         // Salt,
         $"{ nameof(Salt) }.Length:{ (Salt?.Length ?? 0).ToString() }",
         $"{ nameof(ToVerify) }:{ ToVerify }",
     });
 }