summaryrefslogtreecommitdiff
path: root/Assets/Scripts/FileUtil.cs
blob: b0e9b8c0de7bb155b9c30f3ca9764c0d67360013 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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();
  }

}