using System; using System.Text; using System.IO; using System.Security.Cryptography; namespace MotionFramework.Utility { public static class HashUtility { private static string ToString(byte[] hashBytes) { string result = BitConverter.ToString(hashBytes); result = result.Replace("-", ""); return result.ToLower(); } #region SHA1 /// /// 获取字符串的Hash值 /// public static string StringSHA1(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); return BytesSHA1(buffer); } /// /// 获取文件的Hash值 /// public static string FileSHA1(string filePath) { try { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { return StreamSHA1(fs); } } catch (Exception e) { MotionLog.Exception(e.ToString()); return string.Empty; } } /// /// 获取数据流的Hash值 /// public static string StreamSHA1(Stream stream) { // 说明:创建的是SHA1类的实例,生成的是160位的散列码 // 使用 using 语句确保资源正确释放,防止资源泄漏 using (HashAlgorithm hash = HashAlgorithm.Create()) { byte[] hashBytes = hash.ComputeHash(stream); return ToString(hashBytes); } } /// /// 获取字节数组的Hash值 /// public static string BytesSHA1(byte[] buffer) { // 说明:创建的是SHA1类的实例,生成的是160位的散列码 // 使用 using 语句确保资源正确释放,防止资源泄漏 using (HashAlgorithm hash = HashAlgorithm.Create()) { byte[] hashBytes = hash.ComputeHash(buffer); return ToString(hashBytes); } } #endregion #region MD5 /// /// 获取字符串的MD5 /// public static string StringMD5(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); return BytesMD5(buffer); } /// /// 获取文件的MD5 /// public static string FileMD5(string filePath) { try { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { return StreamMD5(fs); } } catch (Exception e) { MotionLog.Exception(e.ToString()); return string.Empty; } } /// /// 获取数据流的MD5 /// public static string StreamMD5(Stream stream) { // 使用 using 语句确保资源正确释放,防止资源泄漏 using (MD5 md5 = MD5.Create()) { byte[] hashBytes = md5.ComputeHash(stream); return ToString(hashBytes); } } /// /// 获取字节数组的MD5 /// public static string BytesMD5(byte[] buffer) { // 使用 using 语句确保资源正确释放,防止资源泄漏 using (MD5 md5 = MD5.Create()) { byte[] hashBytes = md5.ComputeHash(buffer); return ToString(hashBytes); } } #endregion #region CRC32 /// /// 获取字符串的CRC32 /// public static string StringCRC32(string str) { byte[] buffer = Encoding.UTF8.GetBytes(str); return BytesCRC32(buffer); } /// /// 获取文件的CRC32 /// public static string FileCRC32(string filePath) { try { using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read)) { return StreamCRC32(fs); } } catch (Exception e) { MotionLog.Exception(e.ToString()); return string.Empty; } } /// /// 获取数据流的CRC32 /// public static string StreamCRC32(Stream stream) { // 使用 using 语句确保资源正确释放,防止资源泄漏 using (CRC32Algorithm hash = new CRC32Algorithm()) { byte[] hashBytes = hash.ComputeHash(stream); return ToString(hashBytes); } } /// /// 获取字节数组的CRC32 /// public static string BytesCRC32(byte[] buffer) { // 使用 using 语句确保资源正确释放,防止资源泄漏 using (CRC32Algorithm hash = new CRC32Algorithm()) { byte[] hashBytes = hash.ComputeHash(buffer); return ToString(hashBytes); } } #endregion } }