Codwiz51's Wiki

RSS

Navigation







Quick Search
»
Advanced Search »
I had a need for some simple password hashing on a web site. Here's the prototype code.
public partial class Login : System.Web.UI.Page
{
    string Password;

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create the encrypted string
        string password = @"P@ssw0rd1" + @"gharris";
        HashAlgorithm mhash = new SHA1CryptoServiceProvider();
        byte[] bytValue = System.Text.Encoding.UTF8.GetBytes(password);
        byte[] bytHash = mhash.ComputeHash(bytValue);
        mhash.Clear();
        // This string goes into the database
        Password = Convert.ToBase64String(bytHash);


        // Now, make sure we get it back
        // Yep, its the same code.  Just need to make
        // sure we can do it twice and it's the same.
        password = @"P@ssw0rd1" + @"gharris";
        mhash = new SHA1CryptoServiceProvider();
        bytValue = System.Text.Encoding.UTF8.GetBytes(password);
        bytHash = mhash.ComputeHash(bytValue);
        mhash.Clear();
        password = Convert.ToBase64String(bytHash);

        // Normally, Password would be recovered from the database
        if (Password == password)
            Response.Write(@"It worked");
        else
            Response.Write(@"Crud");
    }

ScrewTurn Wiki version 3.0.2.509. Some of the icons created by FamFamFam.