[个人框架 C_Framework] (EasyTool) MD
2018-10-26 本文已影响0人
hh5460
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
public class MD5Tool
{
//PS说明:
//一般MD5默认位32位字符长度
//16位的MD5是从32位的基础上截取中间18位,即切片[8:24]
//64位的MD5是在32位的基础上偶数索引位,即索引位0,2,4,6...的位置前加2个0
//128位的MD5是在32位的基础上偶数索引位,即索引位0,2,4,6...的位置前加6个0
/// <summary>
/// 计算文本内容的16位MD5值
/// </summary>
/// <param name="content">文本内容</param>
/// <param name="isToUpper">True字符全大写 False字符全小写</param>
/// <returns>返回MD5结果</returns>
public static string GetMD5_16(string content, bool isToUpper = true)
{
return getFormatResult(getMD5Bytes(content), isToUpper ? "X2" : "x2").Substring(8, 16);
}
/// <summary>
/// 计算文本内容的MD5值,默认的32位,这个比其他的方法要快一点,BitConverter比循环format要快
/// </summary>
/// <param name="content">文本内容</param>
/// <param name="isToUpper">True字符全大写 False字符全小写</param>
/// <returns>返回MD5结果</returns>
public static string GetMD5(string content, bool isToUpper = true)
{
byte[] md5Bytes = getMD5Bytes(content);
return isToUpper ? BitConverter.ToString(md5Bytes).Replace("-", "").ToUpper() : BitConverter.ToString(md5Bytes).Replace("-", "").ToLower();
}
/// <summary>
/// 直接传入字节流数组,可以对非文本内容进行MD5哈希
/// </summary>
/// <param name="byteContent">字节数组内容</param>
/// <param name="isToUpper">True字符全大写 False字符全小写</param>
/// <returns>返回MD5结果</returns>
public static string GetMD5(byte[] byteContent, bool isToUpper = true)
{
return getFormatResult(byteContent);
}
/// <summary>
/// 计算文件的MD5值
/// </summary>
/// <param name="fileFullPath">文件的完整路径</param>
/// <returns></returns>
public static string GetMD5_File(string fileFullPath)
{
string fileMD5 = string.Empty;
//检查文件是否存在,如果文件存在则进行计算,否则返回空值
if (File.Exists(fileFullPath))
{
using (FileStream fs = new FileStream(fileFullPath, FileMode.Open, FileAccess.Read))
{
fileMD5 = getFormatResult(getMD5Bytes(fs));
}
}
return fileMD5;
}
/// <summary>
/// 计算文本内容的32位MD5值
/// </summary>
/// <param name="content">文本内容</param>
/// <param name="isToUpper">True字符全大写 False字符全小写</param>
/// <returns>返回MD5结果</returns>
public static string GetMD5_32(string content, bool isToUpper = true)
{
return getFormatResult(getMD5Bytes(content), isToUpper ? "X2" : "x2");
}
/// <summary>
/// 获取文本流的MD5字节内容
/// </summary>
/// <param name="content">文本内容</param>
/// <returns>返回MD5字节内容</returns>
static byte[] getMD5Bytes(string content)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(content));
md5.Clear();
return bytes;
}
/// <summary>
/// 获取文件流的MD5字节内容
/// </summary>
/// <param name="fs">文件流</param>
/// <returns>返回MD5字节内容</returns>
static byte[] getMD5Bytes(FileStream fs)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = md5.ComputeHash(fs);
md5.Clear();
return bytes;
}
/// <summary>
/// 获取文件流的MD5字节内容
/// </summary>
/// <param name="fs">文件流</param>
/// <returns>返回MD5字节内容</returns>
static byte[] getMD5Bytes(byte[] byts)
{
MD5 md5 = new MD5CryptoServiceProvider();
byte[] bytes = md5.ComputeHash(byts);
md5.Clear();
return bytes;
}
/// <summary>
/// 获取格式化后的结果
/// </summary>
/// <param name="bytes">字节数组</param>
/// <param name="format">格式化信息</param>
/// <returns></returns>
static string getFormatResult(byte[] bytes, string format="X2")
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
sb.Append(bytes[i].ToString(format));
}
return sb.ToString();
}
}