summaryrefslogtreecommitdiff
path: root/Assets/Scripts/FileUtil.cs
diff options
context:
space:
mode:
Diffstat (limited to 'Assets/Scripts/FileUtil.cs')
-rw-r--r--Assets/Scripts/FileUtil.cs57
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();
+ }
+
+}