Wednesday, September 22, 2010

Basic hashing example

protected void btnhash_Click(object sender, EventArgs e)
{
string salt = CreateSalt(txtpassword.Text.Length);
string pass = CreatePasswordHash(txtpassword.Text.Trim(), salt);
Label1.Text=pass;
}

public 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 string CreatePasswordHash(string pwd, string salt)
{
string saltAndPwd = String.Concat(pwd, salt);
string hashedPwd =
FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");
return hashedPwd;
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home