37 lines
1016 B
C#
Raw Normal View History

using System.Linq;
2023-03-04 15:33:37 -05:00
using System.Security.Cryptography;
using System.Text.RegularExpressions;
using Gitea.Model;
2024-01-19 22:51:06 -05:00
using Serilog;
2023-03-04 15:33:37 -05:00
namespace SPTInstaller.Helpers;
public static class FileHashHelper
2023-03-04 15:33:37 -05:00
{
public static string? GetGiteaReleaseHash(Release release)
2023-03-04 15:33:37 -05:00
{
var regex = Regex.Match(release.Body, @"Release Hash: (?<hash>\S+)");
if (regex.Success)
{
return regex.Groups["hash"].Value;
}
return null;
}
public static bool CheckHash(FileInfo file, string expectedHash)
{
using var md5Service = MD5.Create();
using var sourceStream = file.OpenRead();
var sourceHash = md5Service.ComputeHash(sourceStream);
var expectedHashBytes = Convert.FromBase64String(expectedHash);
2024-01-19 22:51:06 -05:00
Log.Information($"Comparing Hashes :: S: {Convert.ToBase64String(sourceHash)} - E: {expectedHash}");
2023-03-04 15:33:37 -05:00
var matched = Enumerable.SequenceEqual(sourceHash, expectedHashBytes);
2023-03-04 15:33:37 -05:00
return matched;
2023-03-04 15:33:37 -05:00
}
}