You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
4.0 KiB
110 lines
4.0 KiB
using HtmlAgilityPack;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace Infrastructure.Extensions
|
|
{
|
|
public static class StringExtensions
|
|
{
|
|
public static string Format(this string value, params object[] args)
|
|
{
|
|
return string.Format(value, args);
|
|
}
|
|
|
|
public static string GetExtension(this string filename)
|
|
{
|
|
return filename.Substring(filename.LastIndexOf('.') + 1).ToLower();
|
|
}
|
|
|
|
public static string GetSummary(this string value, int length = 32)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(value))
|
|
{
|
|
return value;
|
|
}
|
|
if (value.Length > length)
|
|
{
|
|
return value.Substring(0, length) + "...";
|
|
}
|
|
return value;
|
|
}
|
|
|
|
public static string HtmlToText(this string html, int length = 10)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(html))
|
|
{
|
|
return html;
|
|
}
|
|
var result = string.Empty;
|
|
var doc = new HtmlDocument();
|
|
doc.LoadHtml(html);
|
|
var builder = new StringBuilder();
|
|
builder.Append(result);
|
|
foreach (var item in doc.DocumentNode.ChildNodes)
|
|
{
|
|
builder.Append(item.InnerText);
|
|
if (result.Length >= length)
|
|
{
|
|
result = result.Substring(0, length);
|
|
break;
|
|
}
|
|
}
|
|
result = builder.ToString();
|
|
return result;
|
|
}
|
|
|
|
public static byte[] StringToByteArray(this string hex)
|
|
{
|
|
return Enumerable.Range(0, hex.Length / 2).Select(x => Convert.ToByte(hex.Substring(x * 2, 2), 16)).ToArray();
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5351:不要使用损坏的加密算法", Justification = "<挂起>")]
|
|
public static string DESEncrypt(this string value, string key)
|
|
{
|
|
using var des = new DESCryptoServiceProvider();
|
|
var inputByteArray = Encoding.Default.GetBytes(value);
|
|
var md5Key = key.Md5().Substring(0, 8);
|
|
des.Key = ASCIIEncoding.ASCII.GetBytes(md5Key);
|
|
des.IV = ASCIIEncoding.ASCII.GetBytes(md5Key);
|
|
using var ms = new System.IO.MemoryStream();
|
|
using var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
|
|
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cs.FlushFinalBlock();
|
|
var ret = new StringBuilder();
|
|
foreach (var item in ms.ToArray())
|
|
{
|
|
ret.AppendFormat("{0:X2}", item);
|
|
}
|
|
return ret.ToString();
|
|
}
|
|
|
|
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5351:不要使用损坏的加密算法", Justification = "<挂起>")]
|
|
public static string DESDecrypt(this string value, string key)
|
|
{
|
|
using var des = new DESCryptoServiceProvider();
|
|
var len = value.Length / 2;
|
|
byte[] inputByteArray = new byte[len];
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
inputByteArray[i] = (byte)Convert.ToInt32(value.Substring(i * 2, 2), 16);
|
|
}
|
|
var md5Key = key.Md5().Substring(0, 8);
|
|
des.Key = ASCIIEncoding.ASCII.GetBytes(md5Key);
|
|
des.IV = ASCIIEncoding.ASCII.GetBytes(md5Key);
|
|
using var ms = new System.IO.MemoryStream();
|
|
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
|
|
cs.Write(inputByteArray, 0, inputByteArray.Length);
|
|
cs.FlushFinalBlock();
|
|
return Encoding.Default.GetString(ms.ToArray());
|
|
}
|
|
|
|
public static Guid ToGuid(this string input)
|
|
{
|
|
using MD5 md5 = MD5.Create();
|
|
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
|
|
return new Guid(hash);
|
|
}
|
|
}
|
|
} |