using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security.Cryptography; namespace DataLoader.CS { public class MD5Helper { /// /// 文件MD5校验 /// /// 文件绝对路径 /// MD5校验码 public static string GetMD5Hash(string pathName) { try { FileStream oFileStream = new FileStream(pathName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); MD5CryptoServiceProvider oMD5Hasher = new MD5CryptoServiceProvider(); byte[] arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); // 计算指定Stream 对象的哈希值 oFileStream.Close(); // 由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A” string strHashData = BitConverter.ToString(arrbytHashValue); return strHashData.Replace("-", "").ToLower(); // 替换- } catch (Exception ex) { return ""; } } /// /// 字节数组校验 /// /// 待字节数组 /// MD5校验码 public static string GetMD5Hash(byte[] buffer) { MD5CryptoServiceProvider oMD5Hasher = new MD5CryptoServiceProvider(); try { // 计算指定Stream 对象的哈希值,由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A” byte[] arrbytHashValue = oMD5Hasher.ComputeHash(buffer); string strHashData = BitConverter.ToString(arrbytHashValue); return strHashData.Replace("-", "").ToLower(); // 替换- } catch (Exception ex) { return ""; } } } }