sharp-chat/SharpChat/Extensions.cs
2023-02-07 16:01:56 +01:00

34 lines
1.1 KiB
C#

using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace SharpChat {
public static class Extensions {
public static string GetSignedHash(this string str, string key = null) {
return Encoding.UTF8.GetBytes(str).GetSignedHash(key);
}
public static string GetSignedHash(this byte[] bytes, string key = null) {
key ??= File.Exists(@"login_key.txt") ? File.ReadAllText(@"login_key.txt") : @"woomy";
StringBuilder sb = new();
using(HMACSHA256 algo = new(Encoding.UTF8.GetBytes(key))) {
byte[] hash = algo.ComputeHash(bytes);
foreach(byte b in hash)
sb.AppendFormat(@"{0:x2}", b);
}
return sb.ToString();
}
public static string GetIdString(this byte[] buffer) {
const string id_chars = @"abcdefghijklmnopqrstuvwxyz0123456789-_ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder sb = new();
foreach(byte b in buffer)
sb.Append(id_chars[b % id_chars.Length]);
return sb.ToString();
}
}
}