Please also consider salting your hash (not a culinary concept!). Basically, this means adding some random text to the password before you use it.
" The value of salt helps slow down the dictionary attack attack if your credential store is compromised, giving you extra time to detect and respond to a compromise. "
To save password hashes:
a) Create a random salt value:
byte[] salt = new byte[32]; System.Security.Cryptography.RNGCryptoServiceProvider.Create().GetBytes(salt);
b) Add salt to the password.
// Convert the plain string pwd into bytes byte[] plainTextBytes = System.Text UnicodeEncoding.Unicode.GetBytes(plainText); // Append salt to pwd before hashing byte[] combinedBytes = new byte[plainTextBytes.Length + salt.Length]; System.Buffer.BlockCopy(plainTextBytes, 0, combinedBytes, 0, plainTextBytes.Length); System.Buffer.BlockCopy(salt, 0, combinedBytes, plainTextBytes.Length, salt.Length);
c) Hash the combined password and salt:
// Create hash for the pwd+salt System.Security.Cryptography.HashAlgorithm hashAlgo = new System.Security.Cryptography.SHA256Managed(); byte[] hash = hashAlgo.ComputeHash(combinedBytes);
d) Add salt to the resulting hash.
e) Save the result in the database of your store.
This approach means that you do not need to store the salt separately and then recalculate the hash using the salt value and plaintext password value received from the user.
Change As the original computing power becomes cheaper and faster, the value of hashing or salting ridges decreases. Jeff Atwood has a great 2012 update , too long to be fully repeated here, which says:
This (using salted hashes) will provide the illusion of security more than any real security. Since you need salt and the choice of a hash algorithm to generate the hash, and to verify the hash, it is unlikely that the attacker will have one, and not the other. If you have been compromised to the point where the attacker has your password database, it is reasonable to assume that they have or can get your secret, hidden salt.
The first security rule is to always assume and plan for the worst. Should you use salt, ideally a random salt for each user? Of course, this is definitely good practice, and at least it allows you to disambiguate two users who have the same password. But these days, salts alone can no longer save you from a person who wants to spend several thousand dollars on hardware for a video card, and if you think they can, you have problems.