|
@@ -749,35 +749,35 @@ namespace Masuit.Tools.Security
|
|
|
return Convert.ToBase64String(result); //返回长度为44字节的字符串
|
|
return Convert.ToBase64String(result); //返回长度为44字节的字符串
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- #region MD5加密算法
|
|
|
|
|
|
|
+ #region MD5摘要算法
|
|
|
|
|
|
|
|
- #region 对字符串进行MD5加密
|
|
|
|
|
|
|
+ #region 对字符串进行MD5摘要
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// 对字符串进行MD5加密
|
|
|
|
|
|
|
+ /// 对字符串进行MD5摘要
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- /// <param name="message">需要加密的字符串</param>
|
|
|
|
|
- /// <returns>加密后的结果</returns>
|
|
|
|
|
|
|
+ /// <param name="message">需要摘要的字符串</param>
|
|
|
|
|
+ /// <returns>MD5摘要字符串</returns>
|
|
|
public static string MDString(this string message)
|
|
public static string MDString(this string message)
|
|
|
{
|
|
{
|
|
|
MD5 md5 = MD5.Create();
|
|
MD5 md5 = MD5.Create();
|
|
|
byte[] buffer = Encoding.Default.GetBytes(message);
|
|
byte[] buffer = Encoding.Default.GetBytes(message);
|
|
|
byte[] bytes = md5.ComputeHash(buffer);
|
|
byte[] bytes = md5.ComputeHash(buffer);
|
|
|
- return bytes.Aggregate("", (current, b) => current + b.ToString("x2"));
|
|
|
|
|
|
|
+ return GetHexString(bytes);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// 对字符串进行MD5二次加密
|
|
|
|
|
|
|
+ /// 对字符串进行MD5二次摘要
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- /// <param name="message">需要加密的字符串</param>
|
|
|
|
|
- /// <returns>加密后的结果</returns>
|
|
|
|
|
|
|
+ /// <param name="message">需要摘要的字符串</param>
|
|
|
|
|
+ /// <returns>MD5摘要字符串</returns>
|
|
|
public static string MDString2(this string message) => MDString(MDString(message));
|
|
public static string MDString2(this string message) => MDString(MDString(message));
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// MD5 三次加密算法
|
|
|
|
|
|
|
+ /// MD5 三次摘要算法
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- /// <param name="s">需要加密的字符串</param>
|
|
|
|
|
- /// <returns>MD5字符串</returns>
|
|
|
|
|
|
|
+ /// <param name="s">需要摘要的字符串</param>
|
|
|
|
|
+ /// <returns>MD5摘要字符串</returns>
|
|
|
public static string MDString3(this string s)
|
|
public static string MDString3(this string s)
|
|
|
{
|
|
{
|
|
|
using MD5 md5 = MD5.Create();
|
|
using MD5 md5 = MD5.Create();
|
|
@@ -785,31 +785,31 @@ namespace Masuit.Tools.Security
|
|
|
byte[] bytes1 = md5.ComputeHash(bytes);
|
|
byte[] bytes1 = md5.ComputeHash(bytes);
|
|
|
byte[] bytes2 = md5.ComputeHash(bytes1);
|
|
byte[] bytes2 = md5.ComputeHash(bytes1);
|
|
|
byte[] bytes3 = md5.ComputeHash(bytes2);
|
|
byte[] bytes3 = md5.ComputeHash(bytes2);
|
|
|
- return bytes3.Aggregate("", (current, b) => current + b.ToString("x2"));
|
|
|
|
|
|
|
+ return GetHexString(bytes3);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// 对字符串进行MD5加盐加密
|
|
|
|
|
|
|
+ /// 对字符串进行MD5加盐摘要
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- /// <param name="message">需要加密的字符串</param>
|
|
|
|
|
|
|
+ /// <param name="message">需要摘要的字符串</param>
|
|
|
/// <param name="salt">盐</param>
|
|
/// <param name="salt">盐</param>
|
|
|
- /// <returns>加密后的结果</returns>
|
|
|
|
|
|
|
+ /// <returns>MD5摘要字符串</returns>
|
|
|
public static string MDString(this string message, string salt) => MDString(message + salt);
|
|
public static string MDString(this string message, string salt) => MDString(message + salt);
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// 对字符串进行MD5二次加盐加密
|
|
|
|
|
|
|
+ /// 对字符串进行MD5二次加盐摘要
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- /// <param name="message">需要加密的字符串</param>
|
|
|
|
|
|
|
+ /// <param name="message">需要摘要的字符串</param>
|
|
|
/// <param name="salt">盐</param>
|
|
/// <param name="salt">盐</param>
|
|
|
- /// <returns>加密后的结果</returns>
|
|
|
|
|
|
|
+ /// <returns>MD5摘要字符串</returns>
|
|
|
public static string MDString2(this string message, string salt) => MDString(MDString(message + salt), salt);
|
|
public static string MDString2(this string message, string salt) => MDString(MDString(message + salt), salt);
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// MD5 三次加密算法
|
|
|
|
|
|
|
+ /// MD5 三次摘要算法
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
- /// <param name="s">需要加密的字符串</param>
|
|
|
|
|
|
|
+ /// <param name="s">需要摘要的字符串</param>
|
|
|
/// <param name="salt">盐</param>
|
|
/// <param name="salt">盐</param>
|
|
|
- /// <returns>MD5字符串</returns>
|
|
|
|
|
|
|
+ /// <returns>MD5摘要字符串</returns>
|
|
|
public static string MDString3(this string s, string salt)
|
|
public static string MDString3(this string s, string salt)
|
|
|
{
|
|
{
|
|
|
using MD5 md5 = MD5.Create();
|
|
using MD5 md5 = MD5.Create();
|
|
@@ -817,7 +817,7 @@ namespace Masuit.Tools.Security
|
|
|
byte[] bytes1 = md5.ComputeHash(bytes);
|
|
byte[] bytes1 = md5.ComputeHash(bytes);
|
|
|
byte[] bytes2 = md5.ComputeHash(bytes1);
|
|
byte[] bytes2 = md5.ComputeHash(bytes1);
|
|
|
byte[] bytes3 = md5.ComputeHash(bytes2);
|
|
byte[] bytes3 = md5.ComputeHash(bytes2);
|
|
|
- return bytes3.Aggregate("", (current, b) => current + b.ToString("x2"));
|
|
|
|
|
|
|
+ return GetHexString(bytes3);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
#endregion
|
|
@@ -828,13 +828,13 @@ namespace Masuit.Tools.Security
|
|
|
/// 获取文件的MD5值
|
|
/// 获取文件的MD5值
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <param name="fileName">需要求MD5值的文件的文件名及路径</param>
|
|
/// <param name="fileName">需要求MD5值的文件的文件名及路径</param>
|
|
|
- /// <returns>MD5字符串</returns>
|
|
|
|
|
|
|
+ /// <returns>MD5摘要字符串</returns>
|
|
|
public static string MDFile(this string fileName)
|
|
public static string MDFile(this string fileName)
|
|
|
{
|
|
{
|
|
|
using var fs = new BufferedStream(File.Open(fileName, FileMode.Open, FileAccess.Read), 1048576);
|
|
using var fs = new BufferedStream(File.Open(fileName, FileMode.Open, FileAccess.Read), 1048576);
|
|
|
using MD5 md5 = MD5.Create();
|
|
using MD5 md5 = MD5.Create();
|
|
|
byte[] bytes = md5.ComputeHash(fs);
|
|
byte[] bytes = md5.ComputeHash(fs);
|
|
|
- return bytes.Aggregate("", (current, b) => current + b.ToString("x2"));
|
|
|
|
|
|
|
+ return GetHexString(bytes);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
@@ -851,23 +851,44 @@ namespace Masuit.Tools.Security
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|
|
|
- /// 获取数据流的MD5值
|
|
|
|
|
|
|
+ /// 获取数据流的MD5摘要值
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
/// <param name="stream"></param>
|
|
/// <param name="stream"></param>
|
|
|
- /// <returns>MD5字符串</returns>
|
|
|
|
|
|
|
+ /// <returns>MD5摘要字符串</returns>
|
|
|
public static string MDString(this Stream stream)
|
|
public static string MDString(this Stream stream)
|
|
|
{
|
|
{
|
|
|
using var fs = new BufferedStream(stream, 1048576);
|
|
using var fs = new BufferedStream(stream, 1048576);
|
|
|
using MD5 md5 = MD5.Create();
|
|
using MD5 md5 = MD5.Create();
|
|
|
byte[] bytes = md5.ComputeHash(fs);
|
|
byte[] bytes = md5.ComputeHash(fs);
|
|
|
- var mdstr = bytes.Aggregate("", (current, b) => current + b.ToString("x2"));
|
|
|
|
|
|
|
+ var mdstr = GetHexString(bytes);
|
|
|
stream.Position = 0;
|
|
stream.Position = 0;
|
|
|
return mdstr;
|
|
return mdstr;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ public static string GetHexString(byte[] bytes)
|
|
|
|
|
+ {
|
|
|
|
|
+ var hexArray = new char[bytes.Length << 1];
|
|
|
|
|
+ for (var i = 0; i < hexArray.Length; i += 2)
|
|
|
|
|
+ {
|
|
|
|
|
+ var b = bytes[i >> 1];
|
|
|
|
|
+ hexArray[i] = GetHexValue(b >> 4); // b / 16
|
|
|
|
|
+ hexArray[i + 1] = GetHexValue(b & 0xF); // b % 16
|
|
|
|
|
+ }
|
|
|
|
|
+ return new string(hexArray, 0, hexArray.Length);
|
|
|
|
|
+
|
|
|
|
|
+ static char GetHexValue(int i)
|
|
|
|
|
+ {
|
|
|
|
|
+ if (i < 10)
|
|
|
|
|
+ {
|
|
|
|
|
+ return (char)(i + '0');
|
|
|
|
|
+ }
|
|
|
|
|
+ return (char)(i - 10 + 'a');
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
#endregion
|
|
#endregion
|
|
|
|
|
|
|
|
- #endregion MD5加密算法
|
|
|
|
|
|
|
+ #endregion MD5摘要算法
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// <summary>
|