Sat, 5 September 2009
There are a lot of passwords in my head from numerous sites of which I am a member. I try not to use the same password for any two sites because very often the password I supply is stored in plain text and is therefore available to anyone who can access the sites database. Developers should never allow this to happen, and no one, but you, should ever know your password. As a rule of thumb, if you can request an e-mail which contains your password then it is likely to be stored without encryption or the wrong type.
The best way to store a password is as a Hash, this is a one way cryptographic function that will always give the same result for the same input, so to check a password is valid you simply Hash the input and compare it to the stored value. This means that only the user entering the password will know what it is, the storage will only contain the Hash. This is the way Windows stores passwords.
This is a trivial task, all you need is in the framework and with a simple extension added to the string object, hashing is as easy as falling off a log.
public static class StringExtensions {
/// <summary>
/// <para>Hash a value</para>
/// </summary>
public static string Hash(this string value) {
return value.Hash(false);
}
/// <summary>
/// <para>Hash a value</para>
/// </summary>
public static string Hash(this string value, bool base64Encode) {
var service = new MD5CryptoServiceProvider();
var bytes = service.ComputeHash(Encoding.Default.GetBytes(value));
return base64Encode
? Convert.ToBase64String(bytes)
: Encoding.Default.GetString(bytes);
}
}
A Base64 encoded string will play nice with all databases and xml, so this function contains the option to use it.
var hash = "Test".Hash(true); // Get a Base64 Hash
So what happens when your user forgets their password?
Well there are a few golden rules for this too, given the assumption that the users e-mail is secure enough;
Development, Security
Antix Software Limited is registered in England and Wales.
Registered Number: 3491105 Registered Office: 100-103 Church St., Brighton, BN1 1UJ
13 Jun 2010
Ant
Hashing is not enough you must add salt
http://www.antix.co.uk/A-Developers-Blog/Season-Your-Hash-Adding-Salt