2010-06-21 Atsushi Enomoto <atsushi@ximian.com>
[mcs.git] / class / corlib / System.Security.Policy / HashMembershipCondition.cs
blob6918f250f85c6ba3b6e522512d6eface402663fa
1 //
2 // System.Security.Policy.HashMembershipCondition.cs
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 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 #if !MOONLIGHT
33 using System.Collections;
34 using System.Globalization;
35 using System.Reflection;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Security.Cryptography;
40 using Mono.Security.Cryptography;
42 namespace System.Security.Policy {
44 [Serializable]
45 [ComVisible (true)]
46 public sealed class HashMembershipCondition : IMembershipCondition, IDeserializationCallback, ISerializable {
47 private readonly int version = 1;
49 private HashAlgorithm hash_algorithm;
50 private byte[] hash_value;
52 // so System.Activator.CreateInstance can create an instance...
53 internal HashMembershipCondition ()
57 public HashMembershipCondition (HashAlgorithm hashAlg, byte[] value)
59 if (hashAlg == null)
60 throw new ArgumentNullException ("hashAlg");
61 if (value == null)
62 throw new ArgumentNullException ("value");
64 this.hash_algorithm = hashAlg;
65 this.hash_value = (byte[]) value.Clone ();
69 // Public Properties
72 public HashAlgorithm HashAlgorithm {
73 get {
74 if (hash_algorithm == null)
75 hash_algorithm = new SHA1Managed ();
76 return hash_algorithm;
78 set {
79 if (value == null)
80 throw new ArgumentNullException ("HashAlgorithm");
81 hash_algorithm = value;
85 public byte[] HashValue {
86 get {
87 if (hash_value == null)
88 throw new ArgumentException (Locale.GetText ("No HashValue available."));
89 return (byte[]) hash_value.Clone ();
91 set {
92 if (value == null)
93 throw new ArgumentNullException ("HashValue");
94 hash_value = (byte[]) value.Clone ();
99 // Public Methods
102 public bool Check (Evidence evidence)
104 if (evidence == null)
105 return false;
107 IEnumerator e = evidence.GetHostEnumerator ();
108 while (e.MoveNext ()) {
109 Hash hash = (e.Current as Hash);
110 if (hash == null)
111 continue;
112 if (Compare (hash_value, hash.GenerateHash (hash_algorithm)))
113 return true;
114 break;
116 return false;
119 public IMembershipCondition Copy ()
121 return new HashMembershipCondition (hash_algorithm, hash_value);
124 public override bool Equals (object o)
126 HashMembershipCondition other = (o as HashMembershipCondition);
127 if (other == null)
128 return false;
130 return ((other.HashAlgorithm == hash_algorithm) &&
131 Compare (hash_value, other.hash_value));
134 public SecurityElement ToXml ()
136 return ToXml (null);
139 public SecurityElement ToXml (PolicyLevel level)
141 SecurityElement se = MembershipConditionHelper.Element (typeof (HashMembershipCondition), version);
142 se.AddAttribute ("HashValue", CryptoConvert.ToHex (HashValue));
143 se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
144 return se;
147 public void FromXml (SecurityElement e)
149 FromXml (e, null);
152 public void FromXml (SecurityElement e, PolicyLevel level)
154 MembershipConditionHelper.CheckSecurityElement (e, "e", version, version);
156 hash_value = CryptoConvert.FromHex (e.Attribute ("HashValue"));
158 string algorithm = e.Attribute ("HashAlgorithm");
159 hash_algorithm = (algorithm == null) ? null : HashAlgorithm.Create (algorithm);
162 public override int GetHashCode ()
164 // note: a Copy must have the same hash code
165 int code = hash_algorithm.GetType ().GetHashCode ();
166 if (hash_value != null) {
167 foreach (byte b in hash_value) {
168 code ^= b;
171 return code;
174 public override string ToString ()
176 Type alg_type = this.HashAlgorithm.GetType ();
177 return String.Format ("Hash - {0} {1} = {2}", alg_type.FullName,
178 alg_type.Assembly, CryptoConvert.ToHex (HashValue));
182 // Private Methods
185 private bool Compare (byte[] expected, byte[] actual)
187 if (expected.Length != actual.Length)
188 return false;
190 int len = expected.Length;
191 for (int i = 0; i < len; i++) {
192 if (expected [i] != actual [i])
193 return false;
195 return true;
198 [MonoTODO ("fx 2.0")]
199 void IDeserializationCallback.OnDeserialization (object sender)
203 [MonoTODO ("fx 2.0")]
204 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
210 #endif