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();
  }

}