C#文本转MD5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace FileDialog_Lock
{
    /*
     * using System.Security.Cryptography;
     * */
    public sealed class MD5Hashing
    {
        private static MD5 md5 = MD5.Create();
        //私有化构造函数
        private MD5Hashing()
        {

        }
        /// <summary>
        /// 使用utf8编码将字符串散列
        /// </summary>
        /// <param name="sourceString">要散列的字符串</param>
        /// <returns>散列后的字符串</returns>
        public static string HashString(string sourceString)
        {
            return encode(Encoding.UTF8, sourceString);
        }
        /// <summary>
        /// 使用指定的编码将字符串散列
        /// </summary>
        /// <param name="encode">编码</param>
        /// <param name="sourceString">要散列的字符串</param>
        /// <returns>散列后的字符串</returns>
        private static string encode(Encoding encode, string sourceString)
        {
            byte[] source = md5.ComputeHash(encode.GetBytes(sourceString));
            StringBuilder sBuilder = new StringBuilder();
            for (int i = 0; i < source.Length; i++)
            {
                sBuilder.Append(source[i].ToString("x2"));
            }
            return sBuilder.ToString();
        }      
    }
}


最后修改:2018/03/09 21:59
如果觉得我的文章对你有用,请随意赞赏

发表评论

颜文字