mirror of
https://github.com/bloxstraplabs/bloxstrap.git
synced 2025-04-19 00:51:30 -07:00
64 lines
1.9 KiB
C#
64 lines
1.9 KiB
C#
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
using System.Security.Cryptography;
|
|
using System.Text.Json;
|
|
|
|
namespace Bloxstrap.Helpers
|
|
{
|
|
public class Utilities
|
|
{
|
|
public static void OpenWebsite(string website)
|
|
{
|
|
Process.Start(new ProcessStartInfo { FileName = website, UseShellExecute = true });
|
|
}
|
|
|
|
public static async Task<T?> GetJson<T>(string url)
|
|
{
|
|
using (HttpClient client = new())
|
|
{
|
|
client.DefaultRequestHeaders.Add("User-Agent", Program.ProjectRepository);
|
|
|
|
string json = await client.GetStringAsync(url);
|
|
return JsonSerializer.Deserialize<T>(json);
|
|
}
|
|
}
|
|
|
|
public static string MD5File(string filename)
|
|
{
|
|
using (MD5 md5 = MD5.Create())
|
|
{
|
|
using (FileStream stream = File.OpenRead(filename))
|
|
{
|
|
byte[] hash = md5.ComputeHash(stream);
|
|
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static string MD5Data(byte[] data)
|
|
{
|
|
using (MD5 md5 = MD5.Create())
|
|
{
|
|
byte[] hash = md5.ComputeHash(data);
|
|
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
|
}
|
|
}
|
|
|
|
// quick and hacky way of getting a value from any key/value pair formatted list
|
|
// (command line args, uri params, etc)
|
|
public static string? GetKeyValue(string subject, string key, char delimiter)
|
|
{
|
|
if (subject.LastIndexOf(key) == -1)
|
|
return null;
|
|
|
|
string substr = subject.Substring(subject.LastIndexOf(key) + key.Length);
|
|
|
|
if (!substr.Contains(delimiter))
|
|
return null;
|
|
|
|
return substr.Split(delimiter)[0];
|
|
}
|
|
}
|
|
}
|