bring Mono Security to monotouch
[mcs.git] / class / corlib / System.Security.Policy / Hash.cs
blob60342d12970738db73f8dc671ccc663001c41b43
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-2005 Novell, Inc (http://www.novell.com)
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.
32 #if !NET_2_1 || MONOTOUCH
34 using System.IO;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Security.Cryptography;
39 using System.Security.Permissions;
40 using System.Text;
42 namespace System.Security.Policy {
44 [Serializable]
45 #if NET_2_0
46 [ComVisible (true)]
47 #else
48 [MonoTODO ("(1.x) This doesn't match the MS version perfectly.")]
49 // but it does seems to works exactly like Fx 2.0 beta 1 (and beta2 too) !?!?!
50 #endif
51 public sealed class Hash : ISerializable, IBuiltInEvidence {
53 private Assembly assembly;
54 private byte[] data;
56 internal byte[] _md5;
57 internal byte[] _sha1;
59 public Hash (Assembly assembly)
61 if (assembly == null)
62 throw new ArgumentNullException ("assembly");
63 this.assembly = assembly;
66 internal Hash ()
70 internal Hash (SerializationInfo info, StreamingContext context)
72 data = (byte[]) info.GetValue ("RawData", typeof (byte[]));
76 // Public Properties
79 public byte[] MD5 {
80 get {
81 // Case 1: we have a MD5 value - either precalculated or static (FX 2.0)
82 if (_md5 != null)
83 return _md5;
85 // Case 2: we don't have a MD5 value precalculated so we either
86 // (a): have an assembly reference - and can calculate the hash; or
87 // (b): have been initialized with a static MD5 value (FX 2.0)
88 #if NET_2_0
89 if ((assembly == null) && (_sha1 != null)) {
90 string msg = Locale.GetText ("No assembly data. This instance was initialized with an MSHA1 digest value.");
91 throw new SecurityException (msg);
93 #endif
94 // fully named to avoid conflit between MD5 property and class name
95 HashAlgorithm hash = System.Security.Cryptography.MD5.Create ();
96 _md5 = GenerateHash (hash);
97 return _md5;
101 public byte[] SHA1 {
102 get {
103 // Case 1: we have a SHA1 value - either precalculated or static (FX 2.0)
104 if (_sha1 != null)
105 return _sha1;
107 // Case 2: we don't have a SHA1 value precalculated so we either
108 // (a): have an assembly reference - and can calculate the hash; or
109 // (b): have been initialized with a static MD5 value (FX 2.0)
110 #if NET_2_0
111 if ((assembly == null) && (_md5 != null)) {
112 string msg = Locale.GetText ("No assembly data. This instance was initialized with an MD5 digest value.");
113 throw new SecurityException (msg);
115 #endif
116 // fully named to avoid conflit between SHA1 property and class name
117 HashAlgorithm hash = System.Security.Cryptography.SHA1.Create ();
118 _sha1 = GenerateHash (hash);
119 return _sha1;
124 // Public Methods
127 public byte[] GenerateHash (HashAlgorithm hashAlg)
129 if (hashAlg == null)
130 throw new ArgumentNullException ("hashAlg");
131 return hashAlg.ComputeHash (GetData ());
134 public void GetObjectData (SerializationInfo info, StreamingContext context)
136 if (info == null)
137 throw new ArgumentNullException ("info");
138 info.AddValue ("RawData", GetData ());
141 public override string ToString ()
143 SecurityElement se = new SecurityElement (GetType ().FullName);
144 se.AddAttribute ("version", "1");
146 StringBuilder sb = new StringBuilder ();
147 byte[] raw = GetData ();
148 for (int i=0; i < raw.Length; i++)
149 sb.Append (raw [i].ToString ("X2"));
151 se.AddChild (new SecurityElement ("RawData", sb.ToString ()));
152 return se.ToString ();
156 // Private Methods
159 // FIXME: be more restrictive when imperative security is implemented
160 [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
161 private byte[] GetData ()
163 #if NET_2_0
164 if ((assembly == null) && (data == null)) {
165 string msg = Locale.GetText ("No assembly data.");
166 throw new SecurityException (msg);
168 #endif
169 if (null == data) {
170 // TODO (Pre-Fx-2.0) we mustn't hash the complete assembly!
171 // ---- Look at ToString (MS version) for what to hash (and what not to)
172 // TODO we must drop the authenticode signature (if present)
173 FileStream stream = new
174 FileStream (assembly.Location, FileMode.Open, FileAccess.Read);
175 data = new byte [stream.Length];
176 stream.Read (data, 0, (int)stream.Length);
179 return data;
182 // interface IBuiltInEvidence
184 int IBuiltInEvidence.GetRequiredSize (bool verbose)
186 return (verbose ? 5 : 0); // as documented
189 [MonoTODO ("IBuiltInEvidence")]
190 int IBuiltInEvidence.InitFromBuffer (char [] buffer, int position)
192 return 0;
195 [MonoTODO ("IBuiltInEvidence")]
196 int IBuiltInEvidence.OutputToBuffer (char [] buffer, int position, bool verbose)
198 return 0;
201 #if NET_2_0
202 static public Hash CreateMD5 (byte[] md5)
204 if (md5 == null)
205 throw new ArgumentNullException ("md5");
206 Hash h = new Hash ();
207 h._md5 = md5;
208 return h;
211 static public Hash CreateSHA1 (byte[] sha1)
213 if (sha1 == null)
214 throw new ArgumentNullException ("sha1");
215 Hash h = new Hash ();
216 h._sha1 = sha1;
217 return h;
219 #endif
224 #endif