views:

35

answers:

1

I stored salt and hash values of password during user registration... But during their login i then salt and hash the password given by the user, what happens is a new salt and a new hash is generated....

string password = collection["Password"];
reg.PasswordSalt = CreateSalt(6);
reg.PasswordHash = CreatePasswordHash(password, reg.PasswordSalt);

These statements are in both registration and login....

salt and hash during registration was eVSJE84W and 18DE22FED8C378DB7716B0E4B6C0BA54167315A2

During login it was 4YDIeARH and 12E3C1F4F4CFE04EA973D7C65A09A78E2D80AAC7..... Any suggestion....

    public static string CreateSalt(int size)
    {
        //Generate a cryptographic random number.
        RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
        byte[] buff = new byte[size];
        rng.GetBytes(buff);

        // Return a Base64 string representation of the random number.
        return Convert.ToBase64String(buff);
    }

    public static string CreatePasswordHash(string pwd, string salt)
    {
        string saltAndPwd = String.Concat(pwd, salt);
        string hashedPwd =
         FormsAuthentication.HashPasswordForStoringInConfigFile(
         saltAndPwd, "sha1");

        return hashedPwd;
    }
+3  A: 

Right now you are generating a different salt upon registration and login. You need to use the same salt for hashing or you will get different hashes. That is to say you need store the salt into the database along with the password and reuse it to hash when the user tries to login later.

Steps:

  1. User registers and provides a plain text password
  2. You generate a new random salt and use it to hash the plain text
  3. You store the salt and the hash into the database
  4. Later the user tries to login by providing a new plain text password. You fetch the hash and the salt from database
  5. You use the salt to hash the plain text
  6. Compare the two hashes
Darin Dimitrov
@Darin as of now i am storing `PasswordSalt` and `PasswordHash` in my table.... should i remove `PasswordHash` field from my table?
Pandiya Chendur
Not at all, you need both.
Darin Dimitrov
No, you still need both in the table... but you need to actually *use* them both as well later on, not just the hash. Use the salt, apply it during the hashing process to the password supplied by the user, and then compare that to the stored hash. Don't generate a salt for each login, only for registration.
Amber
Pandiya Chendur
@Darin How to handle this procedure during forget password?
Pandiya Chendur
That basically it it. You need to store sald and hash, then use the same salt on the new user input to generate a hash. Same password, same salt = same hash. Salt is only generated when the user changes a pasword and stored in the database.
TomTom
Forgot password? SImple - generate new (random) password (and new has). Send new password to user. Most websites work like that.
TomTom
You might also want to structure this so that the hashed value is never read back, which makes it harder for it to be leaked by a programming error. All you'd need to do is retrieve the salt, generate a hash, and then pass it to the database, which will do the comparison for you (in a stored proc).
Steven Sudit