2023-12-27 15:33:03 +00:00
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
|
2023-06-11 23:46:02 +00:00
|
|
|
namespace BotSharp.Core.Infrastructures;
|
|
|
|
|
|
|
|
|
|
public static class Utilities
|
|
|
|
|
{
|
2024-06-14 20:39:56 +00:00
|
|
|
public static string HashTextMd5(string text)
|
2023-06-11 23:46:02 +00:00
|
|
|
{
|
|
|
|
|
using var md5 = System.Security.Cryptography.MD5.Create();
|
|
|
|
|
|
2024-06-14 20:39:56 +00:00
|
|
|
var data = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
|
2023-06-11 23:46:02 +00:00
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
foreach (var c in data)
|
|
|
|
|
{
|
|
|
|
|
sb.Append(c.ToString("x2"));
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-14 20:39:56 +00:00
|
|
|
public static string HashTextSha256(string text)
|
|
|
|
|
{
|
|
|
|
|
using var sha256 = System.Security.Cryptography.SHA256.Create();
|
|
|
|
|
|
|
|
|
|
var data = sha256.ComputeHash(Encoding.UTF8.GetBytes(text));
|
|
|
|
|
var sb = new StringBuilder();
|
2024-11-25 06:02:55 +00:00
|
|
|
foreach (var c in data)
|
2024-06-14 20:39:56 +00:00
|
|
|
{
|
|
|
|
|
sb.Append(c.ToString("x2"));
|
|
|
|
|
}
|
|
|
|
|
return sb.ToString();
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-27 09:23:20 +00:00
|
|
|
public static (string, string, string) SplitAsTuple(this string str, string sep)
|
2023-06-11 23:46:02 +00:00
|
|
|
{
|
|
|
|
|
var splits = str.Split(sep);
|
2024-11-27 09:23:20 +00:00
|
|
|
|
|
|
|
|
if (splits.Length == 2 || string.IsNullOrWhiteSpace(splits[2]))
|
|
|
|
|
{
|
|
|
|
|
return (splits[0], splits[1], "CN");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (splits[0], splits[1], splits[2]);
|
2023-06-11 23:46:02 +00:00
|
|
|
}
|
2023-12-27 15:33:03 +00:00
|
|
|
|
2024-01-05 03:20:50 +00:00
|
|
|
/// <summary>
|
|
|
|
|
/// Flush cache
|
|
|
|
|
/// </summary>
|
2023-12-27 15:33:03 +00:00
|
|
|
public static void ClearCache()
|
|
|
|
|
{
|
|
|
|
|
// Clear whole cache.
|
|
|
|
|
if (new MemoryCacheAttribute(0).Cache is MemoryCache memcache)
|
|
|
|
|
{
|
|
|
|
|
memcache.Compact(100);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-25 06:02:55 +00:00
|
|
|
|
|
|
|
|
public static string HideMiddleDigits(string input, bool isEmail = false)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(input))
|
|
|
|
|
{
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isEmail)
|
|
|
|
|
{
|
|
|
|
|
int atIndex = input.IndexOf('@');
|
|
|
|
|
if (atIndex > 1)
|
|
|
|
|
{
|
|
|
|
|
string localPart = input.Substring(0, atIndex);
|
|
|
|
|
if (localPart.Length > 2)
|
|
|
|
|
{
|
|
|
|
|
string maskedLocalPart = $"{localPart[0]}{new string('*', localPart.Length - 2)}{localPart[^1]}";
|
|
|
|
|
return $"{maskedLocalPart}@{input.Substring(atIndex + 1)}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (input.Length > 6)
|
|
|
|
|
{
|
|
|
|
|
return $"{input.Substring(0, 3)}{new string('*', input.Length - 6)}{input.Substring(input.Length - 3)}";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-11 23:46:02 +00:00
|
|
|
}
|