unity中获取文件的MD5码值

例如获取桌面上的测试.txt文件的MD5码值:
	string path = "C://Users/16049/Desktop/测试.txt";
    void Start()
    {
        string fileMd5=GetFileHash(path);
        Debug.Log("测试文件的MD5值:"+fileMd5);
    } 
    
    /// 
    /// 获取文件的MD5码值
    /// 
    /// 文件的路径
    /// 
    public string GetFileHash(string filePath)
    {
        try
        {
            FileStream filestream = new FileStream(filePath,FileMode.Open);
            int length = (int)filestream.Length;
            byte[] data = new byte[length];
            filestream.Read(data,0, length);
            filestream.Close();
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(data);
            string fileMd5 = "";
            foreach (byte item in result)
            {
                fileMd5 += Convert.ToString(item,16);
            }
            return fileMd5;
        }
        catch (FileNotFoundException e)
        {

            return "";
        }
    }

你可能感兴趣的:(C#)