码迷,mamicode.com
首页 > Windows程序 > 详细

C# 获取MD5值

时间:2014-10-24 01:42:43      阅读:258      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   ar   for   sp   div   on   

class CommonHelper
    {
        /// <summary>
        /// 通过字符串获取MD5值,返回32位字符串。
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetMD5String(string str)
        {
            MD5 md5 = MD5.Create();
            byte[] data = Encoding.UTF8.GetBytes(str);
            byte[] data2 = md5.ComputeHash(data);
            
            return GetbyteToString(data2);
            //return BitConverter.ToString(data2).Replace("-", "").ToLower();
        }
        /// <summary>
        /// 获取MD5值。HashAlgorithm.Create("MD5") 或 MD5.Create() HashAlgorithm.Create("SHA256") 或 SHA256.Create()
        /// </summary>
        /// <param name="str"></param>
        /// <param name="hash"></param>
        /// <returns></returns>
        public static string GetMD5String(string str, HashAlgorithm hash)
        {
            byte[] data = Encoding.UTF8.GetBytes(str);
            byte[] data2 = hash.ComputeHash(data);
            return GetbyteToString(data2);
            //return BitConverter.ToString(data2).Replace("-", "").ToLower();
        }

        public static string GetMD5FromFile(string path)
        {
            MD5 md5 = MD5.Create();
            if (!File.Exists(path))
            {
                return "";
            }
            FileStream stream = File.OpenRead(path);
            byte[] data2 = md5.ComputeHash(stream);

            return GetbyteToString(data2);
            //return BitConverter.ToString(data2).Replace("-", "").ToLower();
        }

        private static string GetbyteToString(byte[] data)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < data.Length; i++)
            {
                sb.Append(data[i].ToString("x2"));
            }
            return sb.ToString();
        }
    }

 

C# 获取MD5值

标签:style   blog   color   os   ar   for   sp   div   on   

原文地址:http://www.cnblogs.com/han1982/p/4047281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!