Friday, June 09, 2006

All About Character Encoding - Base64 Encoding

http://dotnet.sys-con.com/read/192527.htm

Instead of using a StringBuilder, use Convert.ToBase64String(result). That will do 6 bits per character instead of 4 (so the output string will be shorter).

You should also use MD5CryptoServiceProvider.Create() rather than ‘new’ because it is faster. Or just MD5.Create() for short.

byte[] input = Encoding.UTF8.GetBytes(inputString);
byte[] output = MD5.Create().ComputeHash(input);
return Convert.ToBase64String(output);

Something like this would be sufficient (and faster). If you want to support Unicode, just type Unicode instead of UTF8.

If you want even faster, keep the MD5 in a static variable so that it only has to be created once.