mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-20 23:54:26 +08:00
changed.对启动器重构的尝试
This commit is contained in:
84
LanMountainDesktop.Launcher/Update/UpdateHash.cs
Normal file
84
LanMountainDesktop.Launcher/Update/UpdateHash.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace LanMountainDesktop.Launcher.Update;
|
||||
|
||||
internal static class UpdateHash
|
||||
{
|
||||
public static string ComputeSha256Hex(string filePath)
|
||||
{
|
||||
using var stream = File.OpenRead(filePath);
|
||||
var hash = SHA256.HashData(stream);
|
||||
return Convert.ToHexString(hash).ToLowerInvariant();
|
||||
}
|
||||
|
||||
public static byte[] ComputeSha512(string filePath)
|
||||
{
|
||||
using var stream = File.OpenRead(filePath);
|
||||
return SHA512.HashData(stream);
|
||||
}
|
||||
|
||||
public static bool TryParseHashBytes(string? rawHash, out byte[] bytes)
|
||||
{
|
||||
bytes = [];
|
||||
if (string.IsNullOrWhiteSpace(rawHash))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var normalized = rawHash.Trim();
|
||||
var separator = normalized.IndexOf(':');
|
||||
if (separator >= 0 && separator < normalized.Length - 1)
|
||||
{
|
||||
normalized = normalized[(separator + 1)..].Trim();
|
||||
}
|
||||
|
||||
var compact = normalized.Replace("-", string.Empty);
|
||||
if (compact.Length > 0 && compact.Length % 2 == 0 && IsHexString(compact))
|
||||
{
|
||||
try
|
||||
{
|
||||
bytes = Convert.FromHexString(compact);
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
bytes = Convert.FromBase64String(normalized);
|
||||
return bytes.Length > 0;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static string NormalizeHashText(string hash)
|
||||
{
|
||||
var normalized = hash.Trim();
|
||||
var separator = normalized.IndexOf(':');
|
||||
if (separator >= 0 && separator < normalized.Length - 1)
|
||||
{
|
||||
normalized = normalized[(separator + 1)..];
|
||||
}
|
||||
|
||||
return normalized.Replace("-", string.Empty).Trim().ToLowerInvariant();
|
||||
}
|
||||
|
||||
private static bool IsHexString(string value)
|
||||
{
|
||||
foreach (var ch in value)
|
||||
{
|
||||
if (!Uri.IsHexDigit(ch))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user