diff options
author | chai <chaifix@163.com> | 2020-03-06 00:30:31 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2020-03-06 00:30:31 +0800 |
commit | 38d4a551880f913a873c3d2c91a98f0434dfa4b2 (patch) | |
tree | a7fd10896449b6596ac464dd3d4881dbc6fd8c82 /Assets/Scripts/FileUtil.cs |
Diffstat (limited to 'Assets/Scripts/FileUtil.cs')
-rw-r--r-- | Assets/Scripts/FileUtil.cs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Assets/Scripts/FileUtil.cs b/Assets/Scripts/FileUtil.cs new file mode 100644 index 0000000..b0e9b8c --- /dev/null +++ b/Assets/Scripts/FileUtil.cs @@ -0,0 +1,57 @@ +using System.Collections; +using System.Collections.Generic; +using System.Security.Cryptography; +using System.IO; +using System; +using UnityEngine; + +public class FileUtil { + + public static string GetMD5Hash(byte[] data) + { + string strHash = ""; + using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) + { + byte[] bytesHash = md5.ComputeHash(data); + strHash = BitConverter.ToString(bytesHash); + strHash = strHash.Replace("-", ""); + } + return strHash.ToLower(); + } + + public static string GetMD5Hash(string pathName) + { + if (!File.Exists(pathName)) + { + Debug.LogError("GetMD5Hash Error, file not exist: " + pathName); + return ""; + } + string strResult = ""; + string strHash = ""; + + byte[] bytesHash; + + FileStream fs = null; + using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider()) + { + try + { + fs = new FileStream(pathName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + + bytesHash = md5.ComputeHash(fs); + fs.Close(); + strHash = BitConverter.ToString(bytesHash); + strHash = strHash.Replace("-", ""); + + strResult = strHash; + } + catch (System.Exception ex) + { + Debug.LogError("read md5 file error :" + pathName + " e: " + ex.ToString()); + } + } + + return strResult.ToLower(); + } + +} |