Tuesday, April 6, 2010

Hasing Passwords

I think some people aren't sure the best way to implement password hashing for storing in a database or config file. Its super fast and super easy.

Simply open snippet compiler an essential tool at ( ) and add a reference to system.web
then add:
using System.Web.Security;

and the code is simply:

string hashedText = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("test", "SHA1");
Console.WriteLine(hashedText);


if you want the full class to use in snippet compiler for copying and pasting it is simply

using System;
using System.Collections.Generic;
using System.Web.Security;
public class HashPassword
{
public static void RunSnippet()
{
string hashedText = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile("test", "SHA1");
Console.WriteLine(hashedText);
}

#region Helper methods

public static void Main()
{
try
{
RunSnippet();
}
catch (Exception e)
{
string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString());
Console.WriteLine(error);
}
finally
{
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}

private static void WL(object text, params object[] args)
{
Console.WriteLine(text.ToString(), args);
}

private static void RL()
{
Console.ReadLine();
}

private static void Break()
{
System.Diagnostics.Debugger.Break();
}

#endregion
}

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.