Kit.Core/LibCommon/Kit.Core.Helpers/AssemblyHelper.cs

56 lines
2.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.Reflection;
namespace Kit.Helpers
{
public class AssemblyHelper
{
public static void CheckSignatures()
{
// Получаем эталонный открытый ключ из текущей сборки
Assembly currentAssembly = Assembly.GetExecutingAssembly();
var referencePublicKey = currentAssembly.GetName().GetPublicKey();
// Проверяем, что текущая сборка подписана
if (referencePublicKey == null || referencePublicKey.Length == 0)
{
throw new ApplicationException("The current build is not signed.");
}
// Получаем список всех загруженных сборок
var assemblies = AppDomain.CurrentDomain.GetAssemblies().OrderBy(x => x.FullName).ToList();
bool anyInvalid = false;
// Проверяем каждую сборку
foreach (var assembly in assemblies)
{
AssemblyName assemblyName = assembly.GetName();
var publicKey = assemblyName.GetPublicKey();
if (assemblyName.Name.ToLowerStartsWith("RiskProf") == false)
{
continue;
}
// Если сборка не подписана
if (publicKey == null || publicKey.Length == 0)
{
anyInvalid = true;
Console.WriteLine($"The assembly {assembly.FullName} is not signed.");
}
// Сравниваем открытый ключ с эталонным
else if (referencePublicKey.SequenceEqual(publicKey) == false)
{
anyInvalid = true;
Console.WriteLine($"The assembly {assembly.FullName} is signed with a different key.");
}
}
if (anyInvalid)
{
throw new ApplicationException("Not all signatures match the signature of the current build.");
}
}
}
}