2004-01-26 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / System.Security.Policy / Hash.cs
1 //
2 // System.Security.Policy.Hash
3 //
4 // Authors:
5 //      Jackson Harper (Jackson@LatitudeGeo.com)
6 //      Sebastien Pouliot (spouliot@motus.com)
7 //
8 // (C) 2002 Jackson Harper, All rights reserved.
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 //
11
12 using System;
13 using System.IO;
14 using System.Text;
15 using System.Reflection;
16 using System.Runtime.Serialization;
17 using System.Security.Cryptography;
18
19 namespace System.Security.Policy {
20
21 [Serializable]
22 public sealed class Hash : ISerializable, IBuiltInEvidence {
23
24         private Assembly assembly;
25         private byte[] data = null;
26
27         public Hash (Assembly assembly) 
28         {
29                 if (assembly == null)
30                         throw new ArgumentNullException ("assembly");
31                 this.assembly = assembly;
32         }
33
34         //
35         // Public Properties
36         //
37
38         public byte[] MD5 {
39                 get {
40                         // fully named to avoid conflit between MD5 property and class name
41                         HashAlgorithm hash = System.Security.Cryptography.MD5.Create ();
42                         return GenerateHash (hash);
43                 }
44         }
45
46         public byte[] SHA1 {
47                 get {
48                         // fully named to avoid conflit between SHA1 property and class name
49                         HashAlgorithm hash = System.Security.Cryptography.SHA1.Create ();
50                         return GenerateHash (hash);
51                 }
52         }
53
54         //
55         // Public Methods
56         //
57
58         public byte[] GenerateHash (HashAlgorithm hashAlg) 
59         {
60                 if (hashAlg == null)
61                         throw new ArgumentNullException ("hashAlg");
62                 return hashAlg.ComputeHash (GetData ());
63         }
64
65         [MonoTODO]
66         public void GetObjectData (SerializationInfo info, StreamingContext context) 
67         {
68                 if (info == null)
69                         throw new ArgumentNullException ("info");
70                 throw new NotImplementedException ();
71         }
72
73         [MonoTODO("The Raw data seems to be different than the raw data I have")]
74         public override string ToString () 
75         {
76                 SecurityElement se = new SecurityElement (GetType ().FullName);
77                 se.AddAttribute ("version", "1");
78                 
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"));
83
84                 se.AddChild (new SecurityElement ("RawData", sb.ToString ()));
85                 return se.ToString ();
86         }
87
88         //
89         // Private Methods
90         //
91
92         [MonoTODO("This doesn't match the MS version perfectly.")]
93         private byte[] GetData () 
94         {
95                 if (null == data) {
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);
103                 }
104
105                 return data;
106         }
107
108         // interface IBuiltInEvidence
109
110         [MonoTODO]
111         int IBuiltInEvidence.GetRequiredSize (bool verbose) 
112         {
113                 return 0;
114         }
115
116         [MonoTODO]
117         int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
118         {
119                 return 0;
120         }
121
122         [MonoTODO]
123         int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
124         {
125                 return 0;
126         }
127 }
128
129 }