BotSharp/src/Infrastructure/BotSharp.Core/Infrastructures/Utilities.cs

51 lines
1.2 KiB
C#
Raw Normal View History

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();
foreach(var c in data)
{
sb.Append(c.ToString("x2"));
}
return sb.ToString();
}
2023-06-11 23:46:02 +00:00
public static (string, string) SplitAsTuple(this string str, string sep)
{
var splits = str.Split(sep);
return (splits[0], splits[1]);
}
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);
}
}
2023-06-11 23:46:02 +00:00
}