C#获取文件MD5值方法

using UnityEngine;
using System.Collections;
using System.IO;
using System.Text;
using System;

public class MD5 : MonoBehaviour
{

    // Use this for initialization
    void Start()
    {
        Debug.Log(GetMD5HashFromFile(Application.streamingAssetsPath + "/t.txt"));
    }

    // Update is called once per frame
    void Update()
    {

    }

    private static string GetMD5HashFromFile(string fileName)
    {
        try
        {
            FileStream file = new FileStream(fileName, FileMode.Open);
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] retVal = md5.ComputeHash(file);
            file.Close();

            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < retVal.Length; i++)
            {
                sb.Append(retVal[i].ToString("x2"));
            }
            return sb.ToString();
        }
        catch (Exception ex)
        {
            throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
        }
    }
}

C#获取文件MD5值方法_第1张图片


原文点击这里

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