Function to return MD5 (string) from string (C#)
· One min read
Functions used
byte[] Encoding.UTF8.GetBytes(string)Converts a string to a byte[]MD5 MD5.Create()Creates an MD5 instancebyte[] md5.ComputeHash(byte[])Generates an MD5 hash from a byte[]
Example
string str2MD5(string src)
{
byte[] srcBytes = Encoding.UTF8.GetBytes(src);
string MD5src;
using (MD5 md5 = MD5.Create())
{
byte[] MD5srcBytes = md5.ComputeHash(srcBytes);
StringBuilder sb = new();
for (int i = 0; i < MD5srcBytes.Length; i++)
sb.Append(MD5srcBytes[i].ToString("x2"));
MD5src = sb.ToString();
}
return MD5src;
}
