How to create md5 file in C #

How to create md5 in c #? I want to call a txt file and call md5 in C # in a txt file for its hash.

public static string MD5Hash(string text)
{
    System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
    return System.Text.RegularExpressions.Regex.Replace(BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(text))), "-", "");
}
+3
source share
1 answer

Stolen straight from here .

protected string GetMD5HashFromFile(string fileName)
{
  FileStream file = new FileStream(fileName, FileMode.Open);
  MD5 md5 = new MD5CryptoServiceProvider();
  byte[] retVal = md5.ComputeHash(file);
  file.Close();

  StringBuilder sb = new StringBuilder();
  for (int i = 0; i < retVal.Length; i++)
  {
    sb.Append(retVal[i].ToString("x2"));
  }
  return sb.ToString();
}
+9
source

All Articles