diff --git a/AutumnBox.OpenFramework.Shared/Open/IMd5Service.cs b/AutumnBox.OpenFramework.Shared/Open/IMd5Service.cs index 50a117d93..1ce19974e 100644 --- a/AutumnBox.OpenFramework.Shared/Open/IMd5Service.cs +++ b/AutumnBox.OpenFramework.Shared/Open/IMd5Service.cs @@ -11,15 +11,15 @@ public interface IMd5Service /// /// 获取某个文件的MD5 /// - /// + /// /// - string GetMd5(string _path); + string GetMd5(string path); /// /// 检查某个文件的MD5是否符合特定MD5 /// - /// - /// + /// + /// /// - bool CheckMd5(string _path, string _md5); + bool CheckMd5(string path, string md5); } } diff --git a/AutumnBox.OpenFramework.Shared/Open/Impl/Md5Impl.cs b/AutumnBox.OpenFramework.Shared/Open/Impl/Md5Impl.cs index 9d76ba5dd..982278c85 100644 --- a/AutumnBox.OpenFramework.Shared/Open/Impl/Md5Impl.cs +++ b/AutumnBox.OpenFramework.Shared/Open/Impl/Md5Impl.cs @@ -5,35 +5,35 @@ namespace AutumnBox.OpenFramework.Open.Impl { + [Obsolete("Use Md5 static class to instead")] class Md5Impl : IMd5Service { /// /// 会返回一个Md5值 /// - /// 需要校验的文件路径 + /// 需要校验的文件路径 /// - public string GetMd5(string _path) + public string GetMd5(string path) { var strResult = "no md5"; - var strHashData = ""; - byte[] arrbytHashValue; - FileStream oFileStream = null; - - System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider(); + System.Security.Cryptography.MD5CryptoServiceProvider oMd5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider(); try { - oFileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值 + var oFileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + var arrbytHashValue = oMd5Hasher.ComputeHash(oFileStream); oFileStream.Close(); //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A” - strHashData = BitConverter.ToString(arrbytHashValue); + var strHashData = BitConverter.ToString(arrbytHashValue); //替换- strHashData = strHashData.Replace("-", ""); strResult = strHashData.ToLower(); } - catch (Exception) { } + catch + { + // ignored + } return strResult; } @@ -41,13 +41,13 @@ public string GetMd5(string _path) /// /// 校验Md5值是否一致 /// - /// 需要校验的文件路径 - /// 需要校验的文件md5值 + /// 需要校验的文件路径 + /// 需要校验的文件md5值 /// - public bool CheckMd5(string _path, string _md5) + public bool CheckMd5(string path, string md5) { - var _fmd5 = GetMd5(_path); - if (_fmd5 == _md5 || _fmd5.ToLower() == _md5) return true; + var fmd5 = GetMd5(path); + if (fmd5.Equals(md5, StringComparison.OrdinalIgnoreCase)) return true; return false; } } diff --git a/AutumnBox.OpenFramework.Shared/Util/Md5.cs b/AutumnBox.OpenFramework.Shared/Util/Md5.cs index d797e4d2a..39b366c55 100644 --- a/AutumnBox.OpenFramework.Shared/Util/Md5.cs +++ b/AutumnBox.OpenFramework.Shared/Util/Md5.cs @@ -1,55 +1,76 @@ using System; using System.IO; +using System.Security.Cryptography; namespace AutumnBox.OpenFramework.Util { /// - /// Md5工具 + /// Md5工具 /// public static class Md5 { /// - /// 会返回一个Md5值 + /// 获取文件的Md5值 /// - /// 需要校验的文件路径 - /// - public static string GetMd5(string _path) + /// 需要校验的文件路径 + /// 小写的Md5值 + public static string GetMd5(string path) { - var strResult = "no md5"; - var strHashData = ""; - - byte[] arrbytHashValue; - FileStream oFileStream = null; - - System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher = new System.Security.Cryptography.MD5CryptoServiceProvider(); + var oFileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + return GetMd5(oFileStream); + } + /// + /// 获取文件流的Md5值 + /// + /// 数据流 + /// 小写的Md5值 + public static string GetMd5(Stream stream) + { try { - oFileStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); - arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream); //计算指定Stream 对象的哈希值 - oFileStream.Close(); - //由以连字符分隔的十六进制对构成的String,其中每一对表示value 中对应的元素;例如“F-2C-4A” - strHashData = BitConverter.ToString(arrbytHashValue); - //替换- + var arrayHashValue = new MD5CryptoServiceProvider().ComputeHash(stream); + var strHashData = BitConverter.ToString(arrayHashValue); strHashData = strHashData.Replace("-", ""); - strResult = strHashData.ToLower(); + return strHashData.ToLower(); } - catch (Exception) { } + catch + { + return "no md5"; + } + } - return strResult; + /// + /// 获取Byte[]的Md5值 + /// + /// Byte[] + /// 小写的Md5值 + public static string GetMd5(byte[] bytes) + { + try + { + var arrayHashValue = new MD5CryptoServiceProvider().ComputeHash(bytes); + var strHashData = BitConverter.ToString(arrayHashValue); + strHashData = strHashData.Replace("-", ""); + return strHashData.ToLower(); + } + catch + { + return "no md5"; + } } /// - /// 校验Md5值是否一致 + /// 校验文件Md5值是否正确 /// - /// 需要校验的文件路径 - /// 需要校验的文件md5值 + /// 需要校验的文件路径 + /// 需要校验的文件md5值 /// - public static bool CheckMd5(string _path, string _md5) + public static bool CheckMd5(string path, string md5) { - var _fmd5 = GetMd5(_path); - if (_fmd5 == _md5 || _fmd5.ToLower() == _md5) return true; + var fmd5 = GetMd5(path); + if (fmd5.Equals(md5, StringComparison.OrdinalIgnoreCase)) return true; return false; } } -} +} \ No newline at end of file