using UnityEngine;
using System.Security.Cryptography;
using System.IO;
using System.Text;
/// <summary> 功能:MD5工具 </summary>
public class MD5Tool : Singleton<MD5Tool> {
/// <summary> 根据路径获取文件的MD5 </summary>
public string BuildFileMd5(string fliePath) {
string filemd5 = null;
try {
using (var fileStream = File.OpenRead(fliePath)) {
var md5 = MD5.Create();
var fileMD5Bytes = md5.ComputeHash(fileStream);//计算指定Stream 对象的哈希值
filemd5 = FormatMD5(fileMD5Bytes);
}
} catch (System.Exception ex) {
Debug.LogError(ex);
}
return filemd5;
}
private byte[] ReadFile(string fileName) {
FileStream pFileStream = null;
byte[] pReadByte = new byte[0];
try {
pFileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);
BinaryReader r = new BinaryReader(pFileStream);
r.BaseStream.Seek(0, SeekOrigin.Begin); //将文件指针设置到文件开
pReadByte = r.ReadBytes((int)r.BaseStream.Length);
return pReadByte;
} catch {
return pReadByte;
} finally {
if (pFileStream != null)
pFileStream.Close();
}
}
/// <summary> 根据路径获取文件解密后的MD5 </summary>
public string BuildFileMd5_AES(string fliePath) {
string filemd5 = null;
try {
byte[] bytes = ReadFile(fliePath);
byte[] bytesAES = AESTool.AESDecrypt(bytes);
var md5 = MD5.Create();
var fileMD5Bytes = md5.ComputeHash(bytesAES);//计算指定Stream 对象的哈希值
filemd5 = FormatMD5(fileMD5Bytes);
} catch (System.Exception ex) {
Debug.LogError(ex);
}
return filemd5;
}
/// <summary> 将byte[]装换成字符串 </summary>
private string FormatMD5(byte[] data) {
return System.BitConverter.ToString(data).Replace("-", "").ToLower();
}
/// <summary> 根据字符串生成MD5 </summary>
public string EncryptWithMD5(string con) {
byte[] sor = Encoding.UTF8.GetBytes(con);
MD5 md5 = MD5.Create();
byte[] result = md5.ComputeHash(sor);
StringBuilder strbul = new StringBuilder(40);
for (int i = 0; i < result.Length; i++) {
strbul.Append(res