Hash.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // System.Security.Policy.Hash
  3. //
  4. // Authors:
  5. // Jackson Harper ([email protected])
  6. // Sebastien Pouliot ([email protected])
  7. //
  8. // (C) 2002 Jackson Harper, All rights reserved.
  9. // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
  10. //
  11. using System;
  12. using System.IO;
  13. using System.Text;
  14. using System.Reflection;
  15. using System.Runtime.Serialization;
  16. using System.Security.Cryptography;
  17. namespace System.Security.Policy {
  18. [Serializable]
  19. public sealed class Hash : ISerializable {
  20. private Assembly assembly;
  21. private byte[] data = null;
  22. public Hash (Assembly assembly)
  23. {
  24. if (assembly == null)
  25. throw new ArgumentNullException ("assembly");
  26. this.assembly = assembly;
  27. }
  28. //
  29. // Public Properties
  30. //
  31. public byte[] MD5 {
  32. get {
  33. // fully named to avoid conflit between MD5 property and class name
  34. HashAlgorithm hash = System.Security.Cryptography.MD5.Create ();
  35. return GenerateHash (hash);
  36. }
  37. }
  38. public byte[] SHA1 {
  39. get {
  40. // fully named to avoid conflit between SHA1 property and class name
  41. HashAlgorithm hash = System.Security.Cryptography.SHA1.Create ();
  42. return GenerateHash (hash);
  43. }
  44. }
  45. //
  46. // Public Methods
  47. //
  48. public byte[] GenerateHash (HashAlgorithm hashAlg)
  49. {
  50. if (hashAlg == null)
  51. throw new ArgumentNullException ("hashAlg");
  52. return hashAlg.ComputeHash (GetData ());
  53. }
  54. [MonoTODO]
  55. public void GetObjectData (SerializationInfo info, StreamingContext context)
  56. {
  57. if (info == null)
  58. throw new ArgumentNullException ("info");
  59. throw new NotImplementedException ();
  60. }
  61. [MonoTODO("The Raw data seems to be different than the raw data I have")]
  62. public override string ToString ()
  63. {
  64. SecurityElement se = new SecurityElement (GetType ().FullName);
  65. se.AddAttribute ("version", "1");
  66. StringBuilder sb = new StringBuilder ();
  67. byte[] raw = GetData ();
  68. for (int i=0; i < raw.Length; i++)
  69. sb.Append (raw [i].ToString ("X2"));
  70. se.AddChild (new SecurityElement ("RawData", sb.ToString ()));
  71. return se.ToString ();
  72. }
  73. //
  74. // Private Methods
  75. //
  76. [MonoTODO("This doesn't match the MS version perfectly.")]
  77. private byte[] GetData ()
  78. {
  79. if (null == data) {
  80. // TODO we mustn't hash the complete assembly!
  81. // ---- Look at ToString (MS version) for what to hash (and what not to)
  82. // TODO we must drop the authenticode signature (if present)
  83. FileStream stream = new
  84. FileStream (assembly.Location, FileMode.Open, FileAccess.Read);
  85. data = new byte [stream.Length];
  86. stream.Read (data, 0, (int)stream.Length);
  87. }
  88. return data;
  89. }
  90. }
  91. }