开发者

How to create a md5 file in c#

开发者 https://www.devze.com 2023-03-09 15:29 出处:网络
How to create a md5 in c#? I want to call the txt file and call the md5 in c# to the txt file to hash it.

How to create a md5 in c#? I want to call the txt file and call the md5 in c# to the txt file to hash it.

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)))开发者_Go百科, “-”, “”);
}


Stolen straight from 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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号