Simply check the file hash value and delete the old pac file

This commit is contained in:
2dust 2025-03-17 11:05:04 +08:00
parent e054d4487d
commit 7cc42ae249
2 changed files with 52 additions and 6 deletions

View file

@ -224,6 +224,13 @@ namespace ServiceLib.Common
}
public static string GetMd5(string str)
{
if (string.IsNullOrEmpty(str))
{
return string.Empty;
}
try
{
var byteOld = Encoding.UTF8.GetBytes(str);
var byteNew = MD5.HashData(byteOld);
@ -235,6 +242,38 @@ namespace ServiceLib.Common
return sb.ToString();
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
return string.Empty;
}
}
public static string GetFileHash(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return string.Empty;
}
if (!File.Exists(filePath))
{
return string.Empty;
}
try
{
using var md5 = MD5.Create();
using var stream = File.OpenRead(filePath);
var hash = md5.ComputeHash(stream);
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
}
catch (Exception ex)
{
Logging.SaveLog(_tag, ex);
return string.Empty;
}
}
/// <summary>
/// idn to idc

View file

@ -33,6 +33,13 @@ namespace ServiceLib.Handler
private static async Task InitText()
{
var path = Path.Combine(_configPath, "pac.txt");
// Delete the old pac file
if (File.Exists(path) && Utils.GetFileHash(path).Equals("b590c07280f058ef05d5394aa2f927fe"))
{
File.Delete(path);
}
if (!File.Exists(path))
{
var pac = EmbedUtils.GetEmbedText(Global.PacFileName);