获取文件的MD5

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using System.Text;
using System.Security.Cryptography;

public class LuaHashComparer
{
    public string GetFileMD5(string path)
    {
        try
        {
            FileStream stream = new FileStream(path, FileMode.Open);
            MD5 mD = new MD5CryptoServiceProvider();
            byte[] retval = mD.ComputeHash(stream);
            stream.Close();

            StringBuilder sc = new StringBuilder();
            for (int i = 0; i < retval.Length; i++)
            {
                //x代表是采用16进制,2代表是两位两位的输出
                //假设有两个数10和26,正常情况十六进制显示0xA、0x1A,这样看起来不整齐,为了好看,可以指定"X2",这样显示出来就是:0x0A、0x1A
                sc.Append(retval[i].ToString("x2"));
            }
            return sc.ToString();
        }
        catch(Exception)
        {
            return string.Empty;
        }
    }
}

 

posted @ 2021-11-08 18:41  搬砖独行者  阅读(248)  评论(0)    收藏  举报