im using HMACSHA256 to generate the hash of password and store it. the objective here is to just authenticate users, i.e - save their hashed passwords, and validate users whenever needed using the hashed passwords
// salt is a guid we store seperately, its the id of the user
byte[] GetHash(byte[] password, byte[] salt)
{
byte[] saltAndPassword = salt.Concat(password).ToArray();
var hmac = HMACSHA256();
hmac.Key = password;
var hash = hmac.ComputeHash(saltAndPassword);
return hash;
}
notice that the key is password, and the hash is generated on salt+password
can we have the password as the key to hash the salt+password, will that comprise security? when using HMACSHA256, do we necessarily have to have a key management inorder to use seperate keys rather than passwords?
also, do keys have to be unique in HMACSHA256?
i know that people often suggest slow functions like - PBKDF2, Scrypt etc..
but is using HMACSHA256 safe too? do people actually use a hmac for hashing and storing passwords? what would be the recommended approach for hashing and storing passwords(if all you have is passwords and unique salts)?
apologies for the parade of questions :) thank you in advance
Edit
I couldnt find a way to comment on neither the question or answer(stackexchange requires me to have a 50 rep). so im posting my comment here :
@CodesInChaos hi, thank you for your answer.
HMAC(password, salt + password)
This is silly but harmless. 1) It uses a variable length value as key. HMAC has some >weird properties when doing that. 2) Why input the password twice?
Hmm, should we do something about it?, to make it a constant length value(any recommendations)?
I thought the hash(salt+password) is always better than hash(password) // the sole purpose of salting??
but from what i see from your answer, HMACSHA256 does similar thing using the key supplied?
If you use HMAC for hashing passwords, I'd use the password as message and the salt as >key. This matches HKDF-Extract.
but, in our case salt is the userid, they are exposed, is it okay to use salt as key which are easy to know?.