2 // System.Security.Policy.HashMembershipCondition
5 // Jackson Harper (Jackson@LatitudeGeo.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2002 Jackson Harper, All rights reserved
9 // Copyright (C) 2004 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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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 using System.Globalization;
32 using System.Reflection;
33 using System.Runtime.Serialization;
34 using System.Security.Cryptography;
37 namespace System.Security.Policy {
41 public sealed class HashMembershipCondition : IMembershipCondition, ISecurityEncodable,
42 ISecurityPolicyEncodable, IDeserializationCallback, ISerializable {
44 public sealed class HashMembershipCondition : IMembershipCondition, ISecurityEncodable,
45 ISecurityPolicyEncodable {
47 private static readonly string XmlTag = "IMembershipCondition";
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 hash_algorithm, byte[] hash_value)
59 if (hash_algorithm == null)
60 throw new ArgumentNullException ("hash_algorithm");
61 if (hash_value == null)
62 throw new ArgumentNullException ("hash_value");
64 this.hash_algorithm = hash_algorithm;
65 this.hash_value = hash_value;
72 public HashAlgorithm HashAlgorithm {
73 get { return hash_algorithm; }
76 throw new ArgumentNullException ();
77 hash_algorithm = value;
81 public byte[] HashValue {
82 get { return hash_value; }
85 throw new ArgumentNullException ();
94 public bool Check (Evidence evidence)
97 throw new ArgumentNullException ("evidence");
99 // Loop through evidence finding the first Hash object
100 foreach (object obj in evidence) {
101 Hash hash = obj as Hash;
104 if (EqualsHashValue (hash.GenerateHash (hash_algorithm)))
111 public IMembershipCondition Copy ()
113 return new HashMembershipCondition (hash_algorithm, hash_value);
116 public override bool Equals (object o)
118 HashMembershipCondition other;
119 if (!(o is HashMembershipCondition))
122 other = (HashMembershipCondition)o;
124 return (other.HashAlgorithm == hash_algorithm &&
125 other.HashValue == hash_value);
128 public SecurityElement ToXml()
133 public SecurityElement ToXml (PolicyLevel level)
135 SecurityElement se = new SecurityElement (XmlTag);
136 Type type = this.GetType ();
137 string classString = type.FullName + ", " + type.Assembly;
138 se.AddAttribute ("class", classString);
139 se.AddAttribute ("version", "1");
140 se.AddAttribute ("HashValue", Encoding.Default.GetString (hash_value));
141 se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
145 public void FromXml (SecurityElement element)
147 FromXml (element, null);
150 public void FromXml (SecurityElement e, PolicyLevel level)
153 throw new ArgumentNullException ("e");
154 if (e.Tag != XmlTag) {
155 throw new ArgumentException ("e", Locale.GetText (
156 "The Tag of SecurityElement must be " + XmlTag));
159 string value = (string)e.Attributes ["HashValue"];
160 string algorithm = (string)e.Attributes ["HashAlgorithm"];
162 if (value == null || algorithm == null ) {
163 throw new ArgumentException ("e", Locale.GetText (
164 "Missing either HashValue or HashAlgorithm"));
167 hash_value = Encoding.Default.GetBytes (value);
168 hash_algorithm = (HashAlgorithm)Assembly.GetExecutingAssembly ().CreateInstance (algorithm);
172 public override int GetHashCode ()
174 return hash_value.GetHashCode ();
177 public override string ToString ()
179 StringBuilder builder = new StringBuilder ();
180 Type alg_type = hash_algorithm.GetType ();
182 builder.Append ("Hash -");
183 builder.AppendFormat ("{0} {1}", alg_type.FullName,
185 builder.AppendFormat (" = ", Encoding.Default.GetString (hash_value));
187 return builder.ToString ();
194 private bool EqualsHashValue (byte[] value)
198 if (value.Length != hash_value.Length)
202 for (int i=0; i<len; i++ ) {
203 if (value[i] != hash_value[i])
212 void IDeserializationCallback.OnDeserialization (object sender)
217 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)