Skip to content

Commit

Permalink
修改Md5类
Browse files Browse the repository at this point in the history
  • Loading branch information
MonoLogueChi committed Jun 9, 2019
1 parent dcc3459 commit f864e5b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 49 deletions.
10 changes: 5 additions & 5 deletions AutumnBox.OpenFramework.Shared/Open/IMd5Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ public interface IMd5Service
/// <summary>
/// 获取某个文件的MD5
/// </summary>
/// <param name="_path"></param>
/// <param name="path"></param>
/// <returns></returns>
string GetMd5(string _path);
string GetMd5(string path);
/// <summary>
/// 检查某个文件的MD5是否符合特定MD5
/// </summary>
/// <param name="_path"></param>
/// <param name="_md5"></param>
/// <param name="path"></param>
/// <param name="md5"></param>
/// <returns></returns>
bool CheckMd5(string _path, string _md5);
bool CheckMd5(string path, string md5);
}
}
32 changes: 16 additions & 16 deletions AutumnBox.OpenFramework.Shared/Open/Impl/Md5Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,49 @@

namespace AutumnBox.OpenFramework.Open.Impl
{
[Obsolete("Use Md5 static class to instead")]
class Md5Impl : IMd5Service
{
/// <summary>
/// 会返回一个Md5值
/// </summary>
/// <param name="_path">需要校验的文件路径</param>
/// <param name="path">需要校验的文件路径</param>
/// <returns></returns>
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;
}

/// <summary>
/// 校验Md5值是否一致
/// </summary>
/// <param name="_path">需要校验的文件路径</param>
/// <param name="_md5">需要校验的文件md5值</param>
/// <param name="path">需要校验的文件路径</param>
/// <param name="md5">需要校验的文件md5值</param>
/// <returns></returns>
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;
}
}
Expand Down
77 changes: 49 additions & 28 deletions AutumnBox.OpenFramework.Shared/Util/Md5.cs
Original file line number Diff line number Diff line change
@@ -1,55 +1,76 @@
using System;
using System.IO;
using System.Security.Cryptography;

namespace AutumnBox.OpenFramework.Util
{
/// <summary>
/// Md5工具
/// Md5工具
/// </summary>
public static class Md5
{
/// <summary>
/// 会返回一个Md5值
/// 获取文件的Md5值
/// </summary>
/// <param name="_path">需要校验的文件路径</param>
/// <returns></returns>
public static string GetMd5(string _path)
/// <param name="path">需要校验的文件路径</param>
/// <returns>小写的Md5值</returns>
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);
}

/// <summary>
/// 获取文件流的Md5值
/// </summary>
/// <param name="stream">数据流</param>
/// <returns>小写的Md5值</returns>
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;
/// <summary>
/// 获取Byte[]的Md5值
/// </summary>
/// <param name="bytes">Byte[]</param>
/// <returns>小写的Md5值</returns>
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";
}
}

/// <summary>
/// 校验Md5值是否一致
/// 校验文件Md5值是否正确
/// </summary>
/// <param name="_path">需要校验的文件路径</param>
/// <param name="_md5">需要校验的文件md5值</param>
/// <param name="path">需要校验的文件路径</param>
/// <param name="md5">需要校验的文件md5值</param>
/// <returns></returns>
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;
}
}
}
}

0 comments on commit f864e5b

Please sign in to comment.