using System.Security.Cryptography;
using System.Text;
using System.IO;

namespace ChuPiao.Common.Core.Security
{
    public class MD5Util
    {
        public static string Encrypt(string strEncrypt, Encoding encoding)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] data = encoding.GetBytes(strEncrypt);
            byte[] result = md5.ComputeHash(data);
            string ret = "";
            for (int i = 0; i < result.Length; i++)
                ret += result[i].ToString("x").PadLeft(2, '0');
            return ret;
        }

        /// <summary> 
        /// 文件转成MD5 
        /// </summary> 
        /// <param name="path">完整路径</param> 
        /// <returns></returns> 
        public static string GetMD5FileString(string path)
        {
            StringBuilder sb = new StringBuilder();
            using (FileStream fs = File.OpenRead(path))
            {
                MD5 md5 = MD5.Create();
                byte[] b = md5.ComputeHash(fs);
                md5.Clear();

                foreach (byte item in b)
                {
                    sb.Append(item.ToString("x2"));
                }

            }
            return sb.ToString();
        } 
    }
}
