using System; using System.Globalization; using System.IO; using System.Reflection; using System.Text; namespace Microsoft.Samples.AppUpdater { public class KeyValidator : MarshalByRefObject { public bool Validate(string assemblyLocation, byte[][] keyList, string[] ExceptionList) { try { if (IsException(assemblyLocation, ExceptionList)) { return true; } Assembly assembly = Assembly.LoadFrom(assemblyLocation); return CompareKeys(assembly.GetName().GetPublicKey(), keyList); } catch (Exception) { return false; } } public static bool IsException(string FilePath, string[] ExceptionList) { if (ExceptionList == null) { return false; } foreach (string text in ExceptionList) { if (Path.GetFileName(FilePath).ToLower(new CultureInfo("en-US")) == text.ToLower(new CultureInfo("en-US"))) { return true; } } return false; } public static bool CompareKeys(byte[] assemblyKey, byte[][] validKeys) { try { ASCIIEncoding aSCIIEncoding = new ASCIIEncoding(); foreach (byte[] bytes in validKeys) { if (aSCIIEncoding.GetString(bytes) == aSCIIEncoding.GetString(assemblyKey)) { return true; } } return false; } catch (Exception) { return false; } } } }