2 // System.Security.Policy.Hash
5 // Jackson Harper (Jackson@LatitudeGeo.com)
6 // Sebastien Pouliot (spouliot@motus.com)
8 // (C) 2002 Jackson Harper, All rights reserved.
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
15 using System.Reflection;
16 using System.Runtime.Serialization;
17 using System.Security.Cryptography;
19 namespace System.Security.Policy {
22 public sealed class Hash : ISerializable {
24 private Assembly assembly;
25 private byte[] data = null;
27 public Hash (Assembly assembly)
30 throw new ArgumentNullException ("assembly");
31 this.assembly = assembly;
40 // fully named to avoid conflit between MD5 property and class name
41 HashAlgorithm hash = System.Security.Cryptography.MD5.Create ();
42 return GenerateHash (hash);
48 // fully named to avoid conflit between SHA1 property and class name
49 HashAlgorithm hash = System.Security.Cryptography.SHA1.Create ();
50 return GenerateHash (hash);
58 public byte[] GenerateHash (HashAlgorithm hashAlg)
61 throw new ArgumentNullException ("hashAlg");
62 return hashAlg.ComputeHash (GetData ());
66 public void GetObjectData (SerializationInfo info, StreamingContext context)
69 throw new ArgumentNullException ("info");
70 throw new NotImplementedException ();
73 [MonoTODO("The Raw data seems to be different than the raw data I have")]
74 public override string ToString ()
76 SecurityElement se = new SecurityElement (GetType ().FullName);
77 se.AddAttribute ("version", "1");
79 StringBuilder sb = new StringBuilder ();
80 byte[] raw = GetData ();
81 for (int i=0; i < raw.Length; i++)
82 sb.Append (raw [i].ToString ("X2"));
84 se.AddChild (new SecurityElement ("RawData", sb.ToString ()));
85 return se.ToString ();
92 [MonoTODO("This doesn't match the MS version perfectly.")]
93 private byte[] GetData ()
96 // TODO we mustn't hash the complete assembly!
97 // ---- Look at ToString (MS version) for what to hash (and what not to)
98 // TODO we must drop the authenticode signature (if present)
99 FileStream stream = new
100 FileStream (assembly.Location, FileMode.Open, FileAccess.Read);
101 data = new byte [stream.Length];
102 stream.Read (data, 0, (int)stream.Length);