wabbajack/Wabbajack.Hashing.xxHash64/StringExtensions.cs

39 lines
978 B
C#
Raw Permalink Normal View History

2021-09-27 12:42:46 +00:00
using System;
using System.Linq;
using System.Text;
2021-12-31 00:50:38 +00:00
using System.Threading.Tasks;
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
namespace Wabbajack.Hashing.xxHash64;
public static class StringExtensions
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
public static string ToHex(this byte[] bytes)
2021-09-27 12:42:46 +00:00
{
2021-10-23 16:51:17 +00:00
var builder = new StringBuilder();
for (var i = 0; i < bytes.Length; i++) builder.Append(bytes[i].ToString("x2"));
return builder.ToString();
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static byte[] FromHex(this string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static string ToBase64(this byte[] data)
{
return Convert.ToBase64String(data);
}
2021-09-27 12:42:46 +00:00
2021-10-23 16:51:17 +00:00
public static byte[] FromBase64(this string data)
{
return Convert.FromBase64String(data);
2021-09-27 12:42:46 +00:00
}
2021-12-31 00:50:38 +00:00
public static async ValueTask<Hash> Hash(this string s)
{
return await Encoding.UTF8.GetBytes(s).Hash();
}
2021-09-27 12:42:46 +00:00
}