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.
iot/projects/Infrastructure/Extensions/StringExtensions.cs

128 lines
4.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using HtmlAgilityPack;
using System;
using System.Globalization;
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(CultureInfo.InvariantCulture, value, args);
}
public static string GetExtension(this string filename)
{
if (filename is null)
{
throw new ArgumentNullException(nameof(filename));
}
return filename.Substring(filename.LastIndexOf('.') + 1).ToLower(CultureInfo.InvariantCulture);
}
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)
{
if (hex is null)
{
throw new ArgumentNullException(nameof(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:<3A><>Ҫʹ<D2AA><CAB9><EFBFBD>𻵵ļ<F0BBB5B5><C4BC><EFBFBD><EFBFBD>㷨", Justification = "<<3C><><EFBFBD><EFBFBD>>")]
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(CultureInfo.InvariantCulture, "{0:X2}", item);
}
return ret.ToString();
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA5351:<3A><>Ҫʹ<D2AA><CAB9><EFBFBD>𻵵ļ<F0BBB5B5><C4BC><EFBFBD><EFBFBD>㷨", Justification = "<<3C><><EFBFBD><EFBFBD>>")]
public static string DESDecrypt(this string value, string key)
{
if (value is null)
{
throw new ArgumentNullException(nameof(value));
}
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();
using var 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)
{
#pragma warning disable CA5351 // <20><>Ҫʹ<D2AA><CAB9><EFBFBD>𻵵ļ<F0BBB5B5><C4BC><EFBFBD><EFBFBD>
using MD5 md5 = MD5.Create();
#pragma warning restore CA5351 // <20><>Ҫʹ<D2AA><CAB9><EFBFBD>𻵵ļ<F0BBB5B5><C4BC><EFBFBD><EFBFBD>
var hash = md5.ComputeHash(Encoding.UTF8.GetBytes(input));
var guid = new Guid(hash);
//list.Add(input, guid.ToString());
return guid;
}
}
}