using System.Data;
using System.Security.Cryptography;
using System.Text;
namespace Net6Demo_Api.Util
{
public static partial class Extention
{
///
/// 转换为MD5加密后的字符串(默认加密为32位)
///
///
///
public static string ToMD5String(this string str)
{
MD5 md5 = MD5.Create();
byte[] inputBytes = Encoding.UTF8.GetBytes(str);
byte[] hashBytes = md5.ComputeHash(inputBytes);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hashBytes.Length; i++)
{
sb.Append(hashBytes[i].ToString("x2"));
}
md5.Dispose();
return sb.ToString();
}
///
/// 转换为MD5加密后的字符串(16位)
///
///
///
public static string ToMD5String16(this string str)
{
return str.ToMD5String().Substring(8, 16);
}
///
/// Base64加密
/// 注:默认采用UTF8编码
///
/// 待加密的明文
/// 加密后的字符串
public static string Base64Encode(this string source)
{
return Base64Encode(source, Encoding.UTF8);
}
///
/// Base64加密
///
/// 待加密的明文
/// 加密采用的编码方式
///
public static string Base64Encode(this string source, Encoding encoding)
{
string encode = string.Empty;
byte[] bytes = encoding.GetBytes(source);
try
{
encode = Convert.ToBase64String(bytes);
}
catch
{
encode = source;
}
return encode;
}
///
/// Base64解密
/// 注:默认使用UTF8编码
///
/// 待解密的密文
/// 解密后的字符串
public static string Base64Decode(this string result)
{
return Base64Decode(result, Encoding.UTF8);
}
///
/// Base64解密
///
/// 待解密的密文
/// 解密采用的编码方式,注意和加密时采用的方式一致
/// 解密后的字符串
public static string Base64Decode(this string result, Encoding encoding)
{
string decode = string.Empty;
byte[] bytes = Convert.FromBase64String(result);
try
{
decode = encoding.GetString(bytes);
}
catch
{
decode = result;
}
return decode;
}
///
/// Base64Url编码
///
/// 待编码的文本字符串
/// 编码的文本字符串
public static string Base64UrlEncode(this string text)
{
var plainTextBytes = Encoding.UTF8.GetBytes(text);
var base64 = Convert.ToBase64String(plainTextBytes).Replace('+', '-').Replace('/', '_').TrimEnd('=');
return base64;
}
///
/// Base64Url解码
///
/// 使用Base64Url编码后的字符串
/// 解码后的内容
public static string Base64UrlDecode(this string base64UrlStr)
{
base64UrlStr = base64UrlStr.Replace('-', '+').Replace('_', '/');
switch (base64UrlStr.Length % 4)
{
case 2:
base64UrlStr += "==";
break;
case 3:
base64UrlStr += "=";
break;
}
var bytes = Convert.FromBase64String(base64UrlStr);
return Encoding.UTF8.GetString(bytes);
}
///
/// 计算SHA1摘要
/// 注:默认使用UTF8编码
///
/// 字符串
///
public static byte[] ToSHA1Bytes(this string str)
{
return str.ToSHA1Bytes(Encoding.UTF8);
}
///
/// 计算SHA1摘要
///
/// 字符串
/// 编码
///
public static byte[] ToSHA1Bytes(this string str, Encoding encoding)
{
SHA1 sha1 = SHA1.Create();
byte[] inputBytes = encoding.GetBytes(str);
byte[] outputBytes = sha1.ComputeHash(inputBytes);
return outputBytes;
}
///
/// 转为SHA1哈希加密字符串
/// 注:默认使用UTF8编码
///
/// 字符串
///
public static string ToSHA1String(this string str)
{
return str.ToSHA1String(Encoding.UTF8);
}
///
/// 转为SHA1哈希
///
/// 字符串
/// 编码
///
public static string ToSHA1String(this string str, Encoding encoding)
{
byte[] sha1Bytes = str.ToSHA1Bytes(encoding);
string resStr = BitConverter.ToString(sha1Bytes);
return resStr.Replace("-", "").ToLower();
}
///
/// SHA256加密
///
/// 字符串
///
public static string ToSHA256String(this string str)
{
byte[] bytes = Encoding.UTF8.GetBytes(str);
byte[] hash = SHA256.Create().ComputeHash(bytes);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
builder.Append(hash[i].ToString("x2"));
}
return builder.ToString();
}
///
/// HMACSHA256算法
///
/// 内容
/// 密钥
///
public static string ToHMACSHA256String(this string text, string secret)
{
secret = secret ?? "";
byte[] keyByte = Encoding.UTF8.GetBytes(secret);
byte[] messageBytes = Encoding.UTF8.GetBytes(text);
using (var hmacsha256 = new HMACSHA256(keyByte))
{
byte[] hashmessage = hmacsha256.ComputeHash(messageBytes);
return Convert.ToBase64String(hashmessage).Replace('+', '-').Replace('/', '_').TrimEnd('=');
}
}
}
}