FileSigner.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Xml;
  7. using System.Diagnostics;
  8. using System.Reflection;
  9. using System.Security.Cryptography;
  10. using Aitex.Core.RT.Log;
  11. namespace Aitex.Core.Util
  12. {
  13. public static class FileSigner
  14. {
  15. public static bool IsValid(string fileName)
  16. {
  17. bool retVal = false;
  18. try
  19. {
  20. XmlDocument doc = new XmlDocument();
  21. doc.Load(fileName);
  22. Debug.Assert(doc != null && doc.DocumentElement != null);
  23. // Get root element
  24. XmlElement elemRoot = doc.DocumentElement;
  25. // Get signature element
  26. XmlElement elemSignature = elemRoot["Signature"];
  27. if (elemSignature == null)
  28. {
  29. return false; // The file was not signed.
  30. }
  31. // Remove signature element from document
  32. elemRoot.RemoveChild(elemSignature);
  33. // Calculate hash code from file (after removing Signature element)
  34. UnicodeEncoding ue = new UnicodeEncoding();
  35. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  36. byte[] signature = sha1.ComputeHash(ue.GetBytes(elemRoot.InnerXml));
  37. string strSignature = Convert.ToBase64String(signature);
  38. // Add signature back to document
  39. elemRoot.AppendChild(elemSignature);
  40. // Compare embedded signature to calculated value
  41. if (elemSignature.InnerText == strSignature)
  42. {
  43. retVal = true;
  44. }
  45. }
  46. catch (System.Exception e)
  47. {
  48. retVal = false;
  49. LOG.Write(e);
  50. }
  51. finally
  52. {
  53. }
  54. return retVal;
  55. }
  56. public static void Sign(string fileName)
  57. {
  58. try
  59. {
  60. XmlDocument doc = new XmlDocument();
  61. bool writeable = true;
  62. if (File.Exists(fileName) && (File.GetAttributes(fileName) & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
  63. {
  64. writeable = false;
  65. File.SetAttributes(fileName, FileAttributes.Normal);
  66. }
  67. doc.Load(fileName);
  68. XmlElement elemRoot = doc.DocumentElement;
  69. // Remove any existing signature
  70. XmlElement elemSignature = elemRoot["Signature"];
  71. if (elemSignature != null)
  72. {
  73. elemRoot.RemoveChild(elemSignature);
  74. }
  75. // Calculate hash code (after removing Signature element)
  76. UnicodeEncoding ue = new UnicodeEncoding();
  77. SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
  78. byte[] nSignature = sha1.ComputeHash(ue.GetBytes(elemRoot.InnerXml));
  79. string strSignature = Convert.ToBase64String(nSignature);
  80. // Add signature to XML document
  81. elemSignature = doc.CreateElement("Signature");
  82. elemSignature.InnerText = strSignature;
  83. elemRoot.AppendChild(elemSignature);
  84. doc.Save(fileName);
  85. if (!writeable)
  86. {
  87. File.SetAttributes(fileName, FileAttributes.ReadOnly);
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. LOG.Write(ex);
  93. }
  94. finally
  95. {
  96. }
  97. }
  98. }
  99. }