En el siguiente ejemplo veremos cómo obtener el hash MD5 de una cadena de texto (String
) en C#. El método nos devolverá otro String
con dicho hash.
Haremos uso de la siguiente biblioteca al principio de nuestra clase:
using System.Security.Cryptography;
public static String GetMD5Hash(String input)
{
MD5CryptoServiceProvider x = new MD5CryptoServiceProvider();
byte[] bs = System.Text.Encoding.UTF8.GetBytes(input);
bs = x.ComputeHash(bs);
System.Text.StringBuilder s = new System.Text.StringBuilder();
foreach (byte b in bs)
{
s.Append(b.ToString("x2").ToLower());
}
String hash = s.ToString();
return hash;
}