MD5Helper的方法

来源:互联网 发布:程序员眼镜 编辑:程序博客网 时间:2024/04/30 12:22

一、字符串使用MD5

 public static string getStringMd5Hash(string str)
        {
            List<string> ls = new List<string>();
            using (MD5 md5 = MD5.Create())
            {
                 byte[] strbytes = Encoding.UTF8.GetBytes(str);
              //  byte[] strbytes = Encoding.GetEncoding("gb2312").GetBytes(str);
                 strbytes= md5.ComputeHash(strbytes);
                 for (int i = 0; i < strbytes.Length; i++)
                 {
                     ls.Add(strbytes[i].ToString("x2"));//将字节转换为字符串
                 }
                // return  Encoding.UTF8.GetString(strbytes);
                 return string.Concat(ls);
            }


二、文件流使用MD5

public static string getFileMd5Hash(string fileName)
         {
             using (MD5 md5 = MD5.Create())
             {
                 using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                 {
                     List<string> lis = new List<string>();
                      byte[] b1=  md5.ComputeHash(fs);
                      for (int i = 0; i < b1.Length; i++)
                      {
                          lis.Add(b1[i].ToString("x2"));
                      }
                      return  string.Concat(lis);
                 }
             }

}

三、中文使用MD5

public static string getChineseMd5Hash(string str)
        {
            List<string> ls = new List<string>();
            using (MD5 md5 = MD5.Create())
            {
                byte[] strbytes = Encoding.GetEncoding("gb2312").GetBytes(str);
                strbytes = md5.ComputeHash(strbytes);
                for (int i = 0; i < strbytes.Length; i++)
                {
                    ls.Add(strbytes[i].ToString("x2"));//将字节转换为字符串
                }
                // return  Encoding.UTF8.GetString(strbytes);
                return string.Concat(ls);

            }

0 0
原创粉丝点击