2023-07-12 09:19:33 +02:00
|
|
|
|
using System.Linq;
|
2023-03-04 15:33:37 -05:00
|
|
|
|
using System.Security.Cryptography;
|
2023-03-06 18:28:02 -05:00
|
|
|
|
using System.Text.RegularExpressions;
|
2023-07-12 09:19:33 +02:00
|
|
|
|
using Gitea.Model;
|
2023-03-04 15:33:37 -05:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
namespace SPTInstaller.Helpers;
|
|
|
|
|
|
|
|
|
|
public static class FileHashHelper
|
2023-03-04 15:33:37 -05:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
public static string? GetGiteaReleaseHash(Release release)
|
2023-03-04 15:33:37 -05:00
|
|
|
|
{
|
2023-07-12 09:19:33 +02:00
|
|
|
|
var regex = Regex.Match(release.Body, @"Release Hash: (?<hash>\S+)");
|
2023-03-06 18:28:02 -05:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
if (regex.Success)
|
|
|
|
|
{
|
|
|
|
|
return regex.Groups["hash"].Value;
|
2023-03-06 18:28:02 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
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);
|
2023-03-04 15:33:37 -05:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
var matched = Enumerable.SequenceEqual(sourceHash, expectedHashBytes);
|
2023-03-04 15:33:37 -05:00
|
|
|
|
|
2023-07-12 09:19:33 +02:00
|
|
|
|
return matched;
|
2023-03-04 15:33:37 -05:00
|
|
|
|
}
|
2023-07-12 09:19:33 +02:00
|
|
|
|
}
|