KeyValidator.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using System.Reflection;
  5. using System.Text;
  6. namespace Microsoft.Samples.AppUpdater
  7. {
  8. public class KeyValidator : MarshalByRefObject
  9. {
  10. public bool Validate(string assemblyLocation, byte[][] keyList, string[] ExceptionList)
  11. {
  12. try
  13. {
  14. if (IsException(assemblyLocation, ExceptionList))
  15. {
  16. return true;
  17. }
  18. Assembly assembly = Assembly.LoadFrom(assemblyLocation);
  19. return CompareKeys(assembly.GetName().GetPublicKey(), keyList);
  20. }
  21. catch (Exception)
  22. {
  23. return false;
  24. }
  25. }
  26. public static bool IsException(string FilePath, string[] ExceptionList)
  27. {
  28. if (ExceptionList == null)
  29. {
  30. return false;
  31. }
  32. foreach (string text in ExceptionList)
  33. {
  34. if (Path.GetFileName(FilePath).ToLower(new CultureInfo("en-US")) == text.ToLower(new CultureInfo("en-US")))
  35. {
  36. return true;
  37. }
  38. }
  39. return false;
  40. }
  41. public static bool CompareKeys(byte[] assemblyKey, byte[][] validKeys)
  42. {
  43. try
  44. {
  45. ASCIIEncoding aSCIIEncoding = new ASCIIEncoding();
  46. foreach (byte[] bytes in validKeys)
  47. {
  48. if (aSCIIEncoding.GetString(bytes) == aSCIIEncoding.GetString(assemblyKey))
  49. {
  50. return true;
  51. }
  52. }
  53. return false;
  54. }
  55. catch (Exception)
  56. {
  57. return false;
  58. }
  59. }
  60. }
  61. }