using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; using System.Text; namespace MOKA_Factory_Tools { /// /// 文件MD5生成类 /// class GetMD5 { public static string GetMD5HashFromFile(string fileName) { try { 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(); } catch (Exception ex) { throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message); } } public static string GetMD5Hash(byte[] bytedata) { try { MD5 md5 = new MD5CryptoServiceProvider(); byte[] retVal = md5.ComputeHash(bytedata); StringBuilder sb = new StringBuilder(); for (int i = 0; i < retVal.Length; i++) { sb.Append(retVal[i].ToString("x2")); } return sb.ToString().ToLower(); } catch (Exception ex) { throw new Exception("GetMD5Hash() fail,error:" + ex.Message); } } public static string GetStrMd5(string ConvertString) { MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); string t2 = BitConverter.ToString(md5.ComputeHash(UTF8Encoding.Default.GetBytes(ConvertString))); t2 = t2.Replace("-", ""); return t2; } } }