首页 > 其他 > 详细

MD5方法

时间:2019-01-13 23:50:07      阅读:176      评论:0      收藏:0      [点我收藏+]
 

  

//计算MD5值的方法 不可逆性
/// <summary>
/// 计算单个字符的MD5方法
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
/// this是扩展的方法 必须定义在static类中

   public static string CalcMD5(this string str) {
            byte[] bytes = System.Text.Encoding.UTF8.GetBytes(str);
            return CalcMD5(bytes);


/// <summary>
/// 计算byte数组的MD5方法
/// </summary>
/// <param name="bytes"></param>
/// <returns></returns>

private static string CalcMD5(byte[] bytes)
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] computeBytes = md5.ComputeHash(bytes);
                string result = "";
                for (int i = 0; i < computeBytes.Length; i++)
                {
                    result += computeBytes[i].ToString("X").Length == 1 ? "0" + computeBytes[i].ToString("X") : computeBytes[i].ToString("X");
                }
                return result;
            }
        }

 

/// <summary>
///计算 Stream流的MD5方法
/// </summary>
/// <param name="stream"></param>
/// <returns></returns>

 private static string CalcMD5(Stream stream )
        {
            using (MD5 md5 = MD5.Create())
            {
                byte[] computeBytes = md5.ComputeHash(stream);
                string result = "";
                for (int i = 0; i < computeBytes.Length; i++)
                {
                    result += computeBytes[i].ToString("X").Length == 1 ? "0" + computeBytes[i].ToString("X") : computeBytes[i].ToString("X");
                }
                return result;
            }
        }

 

MD5方法

原文:https://www.cnblogs.com/x666066/p/10264631.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!