using PdmSwPlugin.Common.Setting;
|
using System;
|
using System.IO;
|
using System.Security.Cryptography;
|
using System.Text;
|
|
namespace PdmSwPlugin.Common.Util
|
{
|
public class MD5Util
|
{
|
public static string Md5SHA1(string filePath)
|
{
|
if (string.IsNullOrEmpty(filePath))
|
{
|
return null;
|
}
|
try
|
{
|
using (FileStream fsRead = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
{
|
SHA1CryptoServiceProvider oSHA1Hasher = new SHA1CryptoServiceProvider();
|
byte[] arrayHashValue = oSHA1Hasher.ComputeHash(fsRead);
|
string sha1ReturnString = BitConverter.ToString(arrayHashValue).Replace("-", "");
|
return sha1ReturnString;
|
}
|
}
|
catch (Exception e)
|
{
|
return null;
|
}
|
}
|
|
public static string Md5PDM(string filePath)
|
{
|
if (string.IsNullOrEmpty(filePath))
|
{
|
return null;
|
}
|
try
|
{
|
using (FileStream fsRead = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
|
{
|
MD5 md5 = new MD5CryptoServiceProvider();
|
byte[] retVal = md5.ComputeHash(fsRead);
|
fsRead.Close();
|
StringBuilder sb = new StringBuilder();
|
for (int i = 0; i < retVal.Length; i++)
|
{
|
sb.Append(retVal[i].ToString("x2"));
|
}
|
return sb.ToString();
|
}
|
}
|
catch (Exception e)
|
{
|
return null;
|
}
|
}
|
|
public static string Get(string filePath)
|
{
|
/// YWT就用SHA1
|
if (PluginSetting.Instance.Customer == "YWT") return Md5SHA1(filePath);
|
// 否则用PDM的
|
return Md5PDM(filePath);
|
}
|
|
public static bool FileEquals(string path1, string path2)
|
{
|
return Get(path1) == Get(path2);
|
}
|
|
public static bool IsFile(string path1, string md5)
|
{
|
return Get(path1) == md5;
|
}
|
|
}
|
}
|