MD5 and SHA-1 Hashing (for String and Files)






1.63/5 (9 votes)
Mar 2, 2006

39560

702
MD5 and SHA-1 Hashing for String and Files using the .NET cryptography Provider
Introduction
This Page deals provides an Example code to show how to implement the .NET MD5 and SHA-1 cryptography Class.
In order to use the MD5 or SHA-1 Class that .NET provides, u must realize that it accepts and returns byte arrays only.
public byte[] ComputeHash ( byte[] buffer , System.Int32 offset , System.Int32 count )
//or public byte[] ComputeHash ( byte[] buffer)
//buffer is the string represented in bytes
this means that u have to convert the string to a byte array, send it to the function and prepare to convert the result byte array back to string.
this was done using this code i found here on code project:
public string encryptString(byte[] RawStringBytes)
{
/// <summary>
/// converts the MD5 of SHA-1 result byte[] to a string
representative
/// </summary>
string hashString ="";
for(int i=0;i<RawStringBytes.Length;i++)
{
hashString += Convert.ToString(RawStringBytes[i], 16).PadLeft(2,'0');
}
return hashString.PadLeft(32,'0');
}
just download and examine the c# coding for more details on how it works.