zoukankan      html  css  js  c++  java
  • C# 生成MD5编码方法(不同位数)

            /// <summary> 
            /// </summary> 
            /// <param name="strSource">待加密字串</param> 
            /// <returns>加密后的字串</returns> 
            public static string MD5Encrypt(string strSource)
            {
                return MD5Encrypt(strSource, 32);
            }

            /// <summary> 
            /// </summary> 
            /// <param name="strSource">待加密字串</param> 
            /// <param name="length">16或32值之一,其它则采用.net默认MD5加密算法</param> 
            /// <returns>加密后的字串</returns> 
            public static string MD5Encrypt(string strSource, int length)
            {
                byte[] bytes = Encoding.ASCII.GetBytes(strSource);
                byte[] hashValue = ((System.Security.Cryptography.HashAlgorithm)System.Security.Cryptography.CryptoConfig.CreateFromName("MD5")).ComputeHash(bytes);
                StringBuilder sb = new StringBuilder();
                switch (length)
                {
                    case 16:
                        for (int i = 4; i < 12; i++)
                            sb.Append(hashValue[i].ToString("x2"));
                        break;
                    case 32:
                        for (int i = 0; i < 16; i++)
                        {
                            sb.Append(hashValue[i].ToString("x2"));
                        }
                        break;
                    default:
                        for (int i = 0; i < hashValue.Length; i++)
                        {
                            sb.Append(hashValue[i].ToString("x2"));
                        }
                        break;
                }
                return sb.ToString();
            } 

  • 相关阅读:
    React网络请求fetch之post请求
    从ajax到fetch到axios
    了解 Fetch API与Fetch+Async/await
    new FormData() 前端上传文件图片到服务器
    ES7之async/await同步编程异步函数
    React网络请求fetch之get请求
    React非受控组件
    React 组件优化之函数防抖节流---使用 debounce +throttle 函数
    【LeetCode】86. Partition List
    【LeetCode】122. Best Time to Buy and Sell Stock II
  • 原文地址:https://www.cnblogs.com/houzuofeng/p/3253187.html
Copyright © 2011-2022 走看看