I'm working on some C# security code and was about to replace it when I saw that it was using the HMACSHA1 class. The code is used to hash a password for storing in the database. The thing that caught my eye was that it uses the password as the HMAC key which is exactly what is computing the hash for. So is using the data for both the key and the thing your hashing OK? Does this make the security stronger or weaker?
psuedo code:
string pwd = "My~!Cra2y~P@ssWord1#123$";
using (HMACSHA1 hasher = new HMACSHA1())
{
hasher.Key = encoding.GetBytes(pwd); // using password for the key
return BytesToHex(hasher.ComputeHash(encoding.GetBytes(pwd))); // computing the hash for the password
}
I wouldn't recommend HMACSHA1 for database password storage, but setting the Key to be the same as the password does weaken the usefulness of the Key in this purpose. The key is supposed to be secret and used to determine if the underlying hashed data has changed.
For passwords you should be using a SALT+Password combination to increase the security of HASH algorithms. I usually use a SALT that is unique to the user, but not the same as the password, such as the user number or initial registration IP address.
Also, keep in mind that SHA1 is no longer recommended as a hashing algorithm.
You can reference MSDN for a clearer understanding.
It's about as strong as an unsalted SHA1 hash with two iterations. i.e. pretty weak.
The lack of salt allows an attack to create rainbow tables, or simply attack all password hashes in your database at the same time.
The low iteration count makes the attack fast, since the attacker can simply try more password candidates.
You should add a salt, and use a slower hashing method, such as PBKDF2 and bcrypt. The .net class Rfc2898DeriveBytes implements PBKDF2, so I recommend using that one.