MD5加密Helper实现

using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Core.Helpers
{
  public static class MD5Helper
  {
    public static string GetStringMd5(string sourceString)
    {
      byte[] hash = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(sourceString));
      string empty = string.Empty;
      for (int index = 0; index < hash.Length; ++index)
        empty += hash[index].ToString("x2");
      return empty.ToUpper();
    }

    public static string GetFileMD5(string filePath)
    {
      using (FileStream fileStream = new FileStream(filePath, FileMode.Open))
        return MD5Helper.GetFileStreamMD5(fileStream);
    }

    public static string GetFileStreamMD5(FileStream fileStream)
    {
      using (MD5 md5 = (MD5) new MD5CryptoServiceProvider())
      {
        byte[] hash = md5.ComputeHash((Stream) fileStream);
        StringBuilder stringBuilder = new StringBuilder();
        for (int index = 0; index < hash.Length; ++index)
          stringBuilder.Append(hash[index].ToString("x2"));
        return stringBuilder.ToString();
      }
    }
  }
}
 

你可能感兴趣的:(立体仓库,哈希算法,算法)