39 lines
1.0 KiB
C#
Raw Normal View History

using Gitea.Model;
using System;
2023-03-04 15:33:37 -05:00
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text.RegularExpressions;
2023-03-04 15:33:37 -05:00
2023-05-11 23:11:39 -04:00
namespace SPTInstaller.Aki.Helper
2023-03-04 15:33:37 -05:00
{
public static class FileHashHelper
{
public static string GetGiteaReleaseHash(Release release)
{
var regex = Regex.Match(release.Body, @"Release Hash: (?<hash>\S+)");
if (regex.Success)
{
return regex.Groups["hash"].Value;
}
return null;
}
2023-03-04 15:33:37 -05:00
public static bool CheckHash(FileInfo file, string expectedHash)
{
using (MD5 md5Service = MD5.Create())
using (var sourceStream = file.OpenRead())
{
byte[] sourceHash = md5Service.ComputeHash(sourceStream);
byte[] expectedHashBytes = Convert.FromBase64String(expectedHash);
bool matched = Enumerable.SequenceEqual(sourceHash, expectedHashBytes);
return matched;
}
}
}
}