2005-01-11 Sebastien Pouliot <sebastien@ximian.com>
[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  <sebastien@ximian.com>
7 //
8 // (C) 2002 Jackson Harper, All rights reserved.
9 // Portions (C) 2002 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.IO;
33 using System.Text;
34 using System.Reflection;
35 using System.Runtime.Serialization;
36 using System.Security.Cryptography;
37
38 namespace System.Security.Policy {
39
40 #if !NET_2_0
41 [MonoTODO("This doesn't match the MS version perfectly.")]
42 // but it does seems to works exactly like Fx 2.0 beta 1 !?!?!
43 #endif
44
45 [Serializable]
46 public sealed class Hash : ISerializable, IBuiltInEvidence {
47
48         private Assembly assembly;
49         private byte[] data;
50
51         internal byte[] _md5;
52         internal byte[] _sha1;
53
54         public Hash (Assembly assembly) 
55         {
56                 if (assembly == null)
57                         throw new ArgumentNullException ("assembly");
58                 this.assembly = assembly;
59         }
60
61         internal Hash () 
62         {
63         }
64
65         internal Hash (SerializationInfo info, StreamingContext context)
66         {
67                 data = (byte[]) info.GetValue ("RawData", typeof (byte[]));
68         }
69
70         //
71         // Public Properties
72         //
73
74         public byte[] MD5 {
75                 get {
76                         // Case 1: we have a MD5 value - either precalculated or static (FX 2.0)
77                         if (_md5 != null)
78                                 return _md5;
79
80                         // Case 2: we don't have a MD5 value precalculated so we either
81                         // (a): have an assembly reference - and can calculate the hash; or
82                         // (b): have been initialized with a static MD5 value (FX 2.0)
83 #if NET_2_0
84                         if ((assembly == null) && (_sha1 != null)) {
85                                 string msg = Locale.GetText ("No assembly data. This instance was initialized with an MSHA1 digest value.");
86                                 throw new SecurityException (msg);
87                         }
88 #endif
89                         // fully named to avoid conflit between MD5 property and class name
90                         HashAlgorithm hash = System.Security.Cryptography.MD5.Create ();
91                         _md5 = GenerateHash (hash);
92                         return _md5;
93                 }
94         }
95
96         public byte[] SHA1 {
97                 get {
98                         // Case 1: we have a SHA1 value - either precalculated or static (FX 2.0)
99                         if (_sha1 != null)
100                                 return _sha1;
101                         
102                         // Case 2: we don't have a SHA1 value precalculated so we either
103                         // (a): have an assembly reference - and can calculate the hash; or
104                         // (b): have been initialized with a static MD5 value (FX 2.0)
105 #if NET_2_0
106                         if ((assembly == null) && (_md5 != null)) {
107                                 string msg = Locale.GetText ("No assembly data. This instance was initialized with an MD5 digest value.");
108                                 throw new SecurityException (msg);
109                         }
110 #endif
111                         // fully named to avoid conflit between SHA1 property and class name
112                         HashAlgorithm hash = System.Security.Cryptography.SHA1.Create ();
113                         _sha1 = GenerateHash (hash);
114                         return _sha1;
115                 }
116         }
117
118         //
119         // Public Methods
120         //
121
122         public byte[] GenerateHash (HashAlgorithm hashAlg) 
123         {
124                 if (hashAlg == null)
125                         throw new ArgumentNullException ("hashAlg");
126                 return hashAlg.ComputeHash (GetData ());
127         }
128
129         public void GetObjectData (SerializationInfo info, StreamingContext context) 
130         {
131                 if (info == null)
132                         throw new ArgumentNullException ("info");
133                 info.AddValue ("RawData", GetData ());
134         }
135
136         public override string ToString () 
137         {
138                 SecurityElement se = new SecurityElement (GetType ().FullName);
139                 se.AddAttribute ("version", "1");
140                 
141                 StringBuilder sb = new StringBuilder ();
142                 byte[] raw = GetData ();
143                 for (int i=0; i < raw.Length; i++)
144                         sb.Append (raw [i].ToString ("X2"));
145
146                 se.AddChild (new SecurityElement ("RawData", sb.ToString ()));
147                 return se.ToString ();
148         }
149
150         //
151         // Private Methods
152         //
153
154         private byte[] GetData () 
155         {
156 #if NET_2_0
157                 if ((assembly == null) && (data == null)) {
158                         string msg = Locale.GetText ("No assembly data.");
159                         throw new SecurityException (msg);
160                 }
161 #endif
162                 if (null == data) {
163                         // TODO (Pre-Fx-2.0) we mustn't hash the complete assembly!
164                         // ---- Look at ToString (MS version) for what to hash (and what not to)
165                         // TODO we must drop the authenticode signature (if present)
166                         FileStream stream = new 
167                                 FileStream (assembly.Location, FileMode.Open, FileAccess.Read);
168                         data = new byte [stream.Length];
169                         stream.Read (data, 0, (int)stream.Length);
170                 }
171
172                 return data;
173         }
174
175         // interface IBuiltInEvidence
176
177         int IBuiltInEvidence.GetRequiredSize (bool verbose) 
178         {
179                 return (verbose ? 5 : 0);       // as documented
180         }
181
182         [MonoTODO]
183         int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position) 
184         {
185                 return 0;
186         }
187
188         [MonoTODO]
189         int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose) 
190         {
191                 return 0;
192         }
193
194 #if NET_2_0
195         static public Hash CreateMD5 (byte[] md5)
196         {
197                 if (md5 == null)
198                         throw new ArgumentNullException ("md5");
199                 Hash h = new Hash ();
200                 h._md5 = md5;
201                 return h;
202         }
203
204         static public Hash CreateSHA1 (byte[] sha1)
205         {
206                 if (sha1 == null)
207                         throw new ArgumentNullException ("sha1");
208                 Hash h = new Hash ();
209                 h._sha1 = sha1;
210                 return h;
211         }
212 #endif
213 }
214
215 }