zoukankan      html  css  js  c++  java
  • MD5方法

     

      

    //计算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;
                }
            }
  • 相关阅读:
    Vagrant安装virtualbox
    SQLSERVER排查CPU占用高的情况
    删除重复记录,只留一条
    ASCII码对应表chr(9)、chr(10)、chr(13)、chr(32)、chr(34)、chr(39)、……
    手机和PC端的录屏软件
    2017年初面试总结
    Python面向对象
    Python字体颜色
    Python第二模块总结
    Fiddler使用教程(转)
  • 原文地址:https://www.cnblogs.com/x666066/p/10264631.html
Copyright © 2011-2022 走看看