Below is a sample code which performs MD5 hash algorithm on a simple string.
public string GetMD5Hash(string input)
{
// Create object of MD5 crypto service provider.
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
// Convert the string into byte array.
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
// Perform ComputeHash on the byte array which will return the hash as an array of 16 bytes.
bs = x.ComputeHash(bs);
//Loop through byte array to convert it into string.
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString(“x2″).ToLower());
}
string password = s.ToString();
return password;
}
The line of code ” b.ToString(“x2″) “ is used to convert number to string of hexadecimal digits.
A more detailed explanation on string formatting will be there in my upcoming post.Till then enjoy coding
