zoukankan      html  css  js  c++  java
  • md5加密篇(一)

        /// <summary>
            /// 获取文件的md5摘要
            /// </summary>
            /// <param name="sFile">文件流</param>
            /// <returns>MD5摘要结果</returns>
            public static string GetAbstractToMD5(Stream sFile)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] result = md5.ComputeHash(sFile);
                StringBuilder sb = new StringBuilder(32);
                for (int i = 0; i < result.Length; i++)
                {
                    sb.Append(result[i].ToString("x").PadLeft(2, '0'));
                }
                return sb.ToString();
            }
    
            /// <summary>
            /// 获取文件的md5摘要
            /// </summary>
            /// <param name="dataFile">文件流</param>
            /// <returns>MD5摘要结果</returns>
            public static string GetAbstractToMD5(byte[] dataFile)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] result = md5.ComputeHash(dataFile);
                StringBuilder sb = new StringBuilder(32);
                for (int i = 0; i < result.Length; i++)
                {
                    sb.Append(result[i].ToString("x").PadLeft(2, '0'));
                }
                return sb.ToString();
            }
    
            /// <summary>
            /// 签名字符串
            /// </summary>
            /// <param name="prestr">需要签名的字符串</param>
            /// <param name="key">密钥</param>
            /// <param name="_input_charset">编码格式</param>
            /// <returns>签名结果</returns>
            public static string Md5Sign(string prestr, string key, string _input_charset)
            {
                StringBuilder sb = new StringBuilder(32);
    
                prestr = prestr + key;
    
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] t = md5.ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
                for (int i = 0; i < t.Length; i++)
                {
                    sb.Append(t[i].ToString("x").PadLeft(2, '0'));
                }
    
                return sb.ToString();
            }
    
    
            /// <summary>
            /// 验证签名
            /// </summary>
            /// <param name="prestr">需要签名的字符串</param>
            /// <param name="sign">签名结果</param>
            /// <param name="key">密钥</param>
            /// <param name="_input_charset">编码格式</param>
            /// <returns>验证结果</returns>
            public static bool Md5Verify(string prestr, string sign, string key, string _input_charset)
            {
                string mysign = Md5Sign(prestr, key, _input_charset);
                if (mysign == sign)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
  • 相关阅读:
    指针
    基本数据类型和string类型的转换
    golang的数据类型之基本数据类型的默认值和转换
    golang的数据类型之字符串类型
    golang的数据类型之布尔类型
    windows pyspider WEB显示框太小解决方法
    MYSQL的全局变量和会话变量
    php 日期计算 总结
    使用 PHPMailer 发送邮件
    MySQL两种表存储结构MyISAM和InnoDB的性能比较测试
  • 原文地址:https://www.cnblogs.com/shy1766IT/p/5399029.html
Copyright © 2011-2022 走看看