I am going to use this newly created method to hash users password. Feel free to give me feedback!
public static byte[] Hash(string strClear, byte[] bytUserSalt, byte[] bytSystemSalt)
{
UTF8Encoding utf8Encoding = new UTF8Encoding();
SHA512Managed sha = new SHA512Managed();
byte[] bytClearString = utf8Encoding.GetBytes(strClear);
byte[] bytHashedUserSaltAndPassword = sha.ComputeHash(bytClearString.Concat(bytUserSalt).ToArray<byte>());
byte[] bytHashedComplete = sha.ComputeHash(bytHashedUserSaltAndPassword.Concat(bytSystemSalt).ToArray<byte>());
return bytHashedComplete;
}
Fork
1 Feedback
What's wrong with the FormsAuthentication.HashPasswordForStoringInConfigFile Method? If anything the name is awesome. - @davidbbitton Wednesday 15, 2010 10:30 PMYou must log in before you can give any feedback
0
This might be be a little cleaner, and you can inject it as a dependancy (stolen from CodeCampServer):
public interface ICryptographer
{
string CreateSalt();
string ComputeHash(string valueToHash);
string GetPasswordHash(string password, string salt);
}
public class Cryptographer : ICryptographer
{
/// <summary>
/// Create salt for encrypting user passwords.
/// Original Source: http://davidhayden.com/blog/dave/archive/2004/02/16/157.aspx
/// </summary>
/// <returns></returns>
public string CreateSalt()
{
int size = 64;
//Generate a cryptographic random number.
var rng = new RNGCryptoServiceProvider();
var buff = new byte[size];
rng.GetBytes(buff);
// Return a Base64 string representation of the random number.
return Convert.ToBase64String(buff);
}
/// <summary>
/// Create a password hash based on a password and salt.
/// Adapted from: http://davidhayden.com/blog/dave/archive/2004/02/16/157.aspx
/// </summary>
/// <returns></returns>
public string ComputeHash(string valueToHash)
{
HashAlgorithm algorithm = SHA512.Create();
byte[] hash = algorithm.ComputeHash(Encoding.UTF8.GetBytes(valueToHash));
return Convert.ToBase64String(hash);
}
public string GetPasswordHash(string password, string salt)
{
return ComputeHash(password + salt);
}
}
0
You must log in before you can post a comment


435
0

Mark '.net' tag as 'like'
Mark '.net' tag as 'ignore'