How to validate salted and hashed password in c#

前端 未结 3 1021
余生分开走
余生分开走 2020-12-16 06:55

I used the below method to salt and hash the passwords

public string CreateSalt(int size)
{
    var rng = new System.Security.Cryptography.RNGCryptoServicePr         


        
3条回答
  •  误落风尘
    2020-12-16 07:52

    Create an column in your user table Username and Hash and Salt

    User Register

    1) Take input username or password from user in your registration form.

    2) Create Hash and Salt for input password with below method.

    public class HashSalt
    {
        public string Hash { get; set; }
        public string Salt { get; set; }
    }
    
    public static HashSalt GenerateSaltedHash(int size, string password)
    {
        var saltBytes = new byte[size];
        var provider = new RNGCryptoServiceProvider();
        provider.GetNonZeroBytes(saltBytes);
        var salt = Convert.ToBase64String(saltBytes);
    
        var rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, saltBytes, 10000);
        var hashPassword = Convert.ToBase64String(rfc2898DeriveBytes.GetBytes(256));
    
        HashSalt hashSalt = new HashSalt { Hash = hashPassword, Salt = salt };
        return hashSalt;
    }
    

    Rfc2898DeriveBytes class is used to generate the hash using the RFC2898 specification, which uses a method known as PBKDF2 (Password Based Key Derivation Function #2) and is currently recommend by the IETF (Internet Engineering Task Force) for new applications.

    3) Then stored this Hash and Salt with user record in database.

    public void Submit1_click(object sender, EventArgs r)
    {
        //Your code here
    
        HashSalt hashSalt = GenerateSaltedHash(64, password1.Text);
    
        //Your code here
    
        cmd.Parameters.AddWithValue("@hash", hashSalt.Hash);
        cmd.Parameters.AddWithValue("@salt", hashSalt.Salt);
    
        //You code here
    }
    

    User Login

    1) Take input username or password from user in your login form.

    2) In Login_click get user by username from database.

    3) Pass stored Hash and Salt to below function.

    public static bool VerifyPassword(string enteredPassword, string storedHash, string storedSalt)
    {
        var saltBytes = Convert.FromBase64String(storedSalt);
        var rfc2898DeriveBytes = new Rfc2898DeriveBytes(enteredPassword, saltBytes, 10000);
        return Convert.ToBase64String(rfc2898DeriveBytes.GetBytes(256)) == storedHash;
    }
    

    4) Then login your user by verifying his/her password.

    public void Login_click(object sender, EventArgs r)
    {
        //You code here
    
        User user = GetUserByUsername(txtUsername.Text);
    
        bool isPasswordMatched = VerifyPassword(txtpassword.Text, user.Hash, user.Salt);
    
        if (isPasswordMatched)
        {
            //Login Successfull
        }
        else
        {
            //Login Failed
        }
    
        //Your code here
    }
    

    Reference: Effective Password Hashing

提交回复
热议问题