123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- /**
- *┌──────────────────────────────────────────────────────────────┐
- *│ 描 述:日志相关的工具类
- *│ 作 者:执笔小白
- *│ 版 本:1.0
- *│ 创建时间:2020-6-13 15:40:56
- *└──────────────────────────────────────────────────────────────┘
- *┌──────────────────────────────────────────────────────────────┐
- *│ 命名空间: WMSTOMESTT
- *│ 类 名:ETools
- *└──────────────────────────────────────────────────────────────┘
- */
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- namespace Fa_Xiaomi_N801A.MainForm.EDEHelper
- {
- public class Md5Helper
- {
- /// <summary>
- /// 计算字符串的MD5
- /// </summary>
- /// <param name="str">需要计算的字符串</param>
- /// <param name="md5_16">需不需要返回16位,默认32位</param>
- /// <param name="capital">需不需要返回大写,默认小写</param>
- /// <returns></returns>
- public static string GetMD5Value_String(string str = "", bool md5_16 = false, bool capital = false)
- {
- string pwd = "";
- MD5 md5 = MD5.Create(); //实例化一个md5对象
- byte[] s = md5.ComputeHash(UTF8Encoding.UTF8.GetBytes(str));
- // 大写
- if (capital)
- {
- for (int i = 0; i < s.Length; i++)
- {
- // --X为十六进制,2为每次都是两位数
- pwd = pwd + s[i].ToString("X2"); // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母
- }
- }
- else
- {
- for (int i = 0; i < s.Length; i++) // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
- {
- pwd = pwd + s[i].ToString("X2").ToLower();
- }
- }
- // 16位
- if (md5_16)
- {
- pwd = pwd.Substring(4, 16);
- }
- return pwd;
- }
- /// <summary>
- /// 计算文件的MD5值
- /// </summary>
- /// <param name="fileName">需要计算md5值的文件的路径</param>
- /// <param name="md5_16">需不需要返回16位,默认32位</param>
- /// <param name="capital">需不需要返回大写,默认小写</param>
- /// <returns></returns>
- public static string GetMD5Value_File(string fileName = "", bool md5_16 = false, bool capital = false)
- {
- string pwd = "";
- MD5 md5 = MD5.Create(); //实例化一个md5对象
- using (FileStream file = new FileStream(fileName, System.IO.FileMode.Open))
- {
- byte[] s = md5.ComputeHash(file);
- file.Close();
- // 大写
- if (capital)
- {
- for (int i = 0; i < s.Length; i++)
- {
- // --X为十六进制,2为每次都是两位数
- pwd = pwd + s[i].ToString("X2"); // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母
- }
- }
- else
- {
- for (int i = 0; i < s.Length; i++) // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母,如果使用大写(X)则格式后的字符是大写字符
- {
- pwd = pwd + s[i].ToString("X2").ToLower();
- }
- }
- // 16位
- if (md5_16)
- {
- pwd = pwd.Substring(4, 16);
- }
- }
- return pwd;
- }
- }
- }
|