2 // System.Security.Policy.HashMembershipCondition
5 // Jackson Harper (Jackson@LatitudeGeo.com)
7 // (C) 2002 Jackson Harper, All rights reserved
11 using System.Reflection;
12 using System.Security.Cryptography;
14 namespace System.Security.Policy {
17 public sealed class HashMembershipCondition : IMembershipCondition,
18 ISecurityEncodable, ISecurityPolicyEncodable {
20 private static readonly string XmlTag = "IMembershipCondition";
22 private HashAlgorithm hash_algorithm;
23 private byte[] hash_value;
25 public HashMembershipCondition (HashAlgorithm hash_algorithm,
28 if (hash_algorithm == null || hash_value == null)
29 throw new ArgumentNullException ();
31 this.hash_algorithm = hash_algorithm;
32 this.hash_value = hash_value;
39 public HashAlgorithm HashAlgorithm {
40 get { return hash_algorithm; }
43 throw new ArgumentNullException ();
44 hash_algorithm = value;
48 public byte[] HashValue {
49 get { return hash_value; }
52 throw new ArgumentNullException ();
61 public bool Check (Evidence evidence)
64 throw new ArgumentNullException ();
66 // Loop through evidence finding the first Hash object
67 foreach (object obj in evidence) {
68 Hash hash = obj as Hash;
71 if (EqualsHashValue (hash.GenerateHash (hash_algorithm)))
78 public IMembershipCondition Copy ()
80 return new HashMembershipCondition (hash_algorithm, hash_value);
83 public override bool Equals (object o)
85 HashMembershipCondition other;
86 if (!(o is HashMembershipCondition))
89 other = (HashMembershipCondition)o;
91 return (other.HashAlgorithm == hash_algorithm &&
92 other.HashValue == hash_value);
95 public SecurityElement ToXml()
100 public SecurityElement ToXml (PolicyLevel level)
102 SecurityElement se = new SecurityElement (XmlTag);
103 Type type = this.GetType ();
104 string classString = type.FullName + ", " + type.Assembly;
105 se.AddAttribute ("class", classString);
106 se.AddAttribute ("version", "1");
107 se.AddAttribute ("HashValue", Encoding.Default.GetString (hash_value));
108 se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
112 public void FromXml (SecurityElement element)
114 FromXml (element, null);
117 public void FromXml (SecurityElement e,
121 throw new ArgumentNullException ();
123 throw new ArgumentException(
124 "e","The Tag of SecurityElement must be " + XmlTag);
126 string value = (string)e.Attributes["HashValue"];
127 string algorithm = (string)e.Attributes["HashAlgorithm"];
129 if (value == null || algorithm == null )
130 throw new ArgumentException ();
132 hash_value = Encoding.Default.GetBytes (value);
133 hash_algorithm = (HashAlgorithm)Assembly.GetExecutingAssembly ().CreateInstance (algorithm);
137 [MonoTODO("This is not right")]
138 public override int GetHashCode ()
140 return hash_value.GetHashCode ();
143 public override string ToString ()
145 StringBuilder builder = new StringBuilder ();
146 Type alg_type = hash_algorithm.GetType ();
148 builder.Append ("Hash -");
149 builder.AppendFormat ("{0} {1}", alg_type.FullName,
151 builder.AppendFormat (" = ", Encoding.Default.GetString (hash_value));
153 return builder.ToString ();
160 private bool EqualsHashValue (byte[] value)
164 if (value.Length != hash_value.Length)
168 for (int i=0; i<len; i++ ) {
169 if (value[i] != hash_value[i])