» MD5 Encrypt String to 32 Character Hash by sde |
|
(Login to remove green text ads)
Snippet
This is more of a code snippet than an article, but I was looking for a way to match a php md5 encrypted string in C# and came up with this.
Code:
private string encryptString(string strToEncrypt)
{
System.Text.UTF8Encoding ue = new System.Text.UTF8Encoding();
byte[] bytes = ue.GetBytes(strToEncrypt);
// encrypt bytes
System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hashBytes = md5.ComputeHash(bytes);
// Convert the encrypted bytes back to a string (base 16)
string hashString = "";
for(int i=0;i<hashBytes.Length;i++)
{
hashString += Convert.ToString(hashBytes[i],16).PadLeft(2,'0');
}
return hashString.PadLeft(32,'0');
}
Sorry for the lack of comments, but hopefully this will help someone =)
|
|