2004-01-26 Sebastien Pouliot <spouliot@videotron.ca>
[mono.git] / mcs / class / corlib / System.Security.Policy / HashMembershipCondition.cs
1 //
2 // System.Security.Policy.HashMembershipCondition
3 //
4 // Author(s):
5 //  Jackson Harper (Jackson@LatitudeGeo.com)
6 //
7 // (C) 2002 Jackson Harper, All rights reserved
8 //
9
10 using System.Text;
11 using System.Reflection;
12 using System.Security.Cryptography;
13
14 namespace System.Security.Policy {
15
16         [Serializable]
17         public sealed class HashMembershipCondition : IMembershipCondition, 
18                 ISecurityEncodable, ISecurityPolicyEncodable {
19
20                 private static readonly string XmlTag = "IMembershipCondition";
21
22                 private HashAlgorithm hash_algorithm;
23                 private byte[] hash_value;
24
25                 public HashMembershipCondition (HashAlgorithm hash_algorithm,
26                         byte[] hash_value)
27                 {
28                         if (hash_algorithm == null || hash_value == null)
29                                 throw new ArgumentNullException ();
30                                 
31                         this.hash_algorithm = hash_algorithm;
32                         this.hash_value = hash_value;
33                 }
34
35                 //
36                 // Public Properties
37                 //
38                 
39                 public HashAlgorithm HashAlgorithm {
40                         get { return hash_algorithm; }
41                         set { 
42                                 if (value == null)
43                                         throw new ArgumentNullException ();
44                                 hash_algorithm = value; 
45                         }
46                 }
47
48                 public byte[] HashValue {
49                         get { return hash_value; }
50                         set { 
51                                 if (value == null)
52                                         throw new ArgumentNullException ();
53                                 hash_value = value; 
54                         } 
55                 }
56
57                 //
58                 // Public Methods
59                 //
60
61                 public bool Check (Evidence evidence)
62                 {
63                         if (evidence == null)
64                                 throw new ArgumentNullException ();
65
66                         // Loop through evidence finding the first Hash object
67                         foreach (object obj in evidence) {
68                                 Hash hash = obj as Hash;
69                                 if (hash == null)
70                                         continue;
71                                 if (EqualsHashValue (hash.GenerateHash (hash_algorithm)))
72                                         return true;
73                                 break;
74                         }
75                         return false;
76                 }
77
78                 public IMembershipCondition Copy ()
79                 {
80                         return new HashMembershipCondition (hash_algorithm, hash_value);
81                 }
82
83                 public override bool Equals (object o)
84                 {
85                         HashMembershipCondition other;
86                         if (!(o is HashMembershipCondition))
87                                 return false;
88
89                         other = (HashMembershipCondition)o;
90                         
91                         return (other.HashAlgorithm == hash_algorithm &&
92                                 other.HashValue == hash_value);
93                 }
94                 
95                 public SecurityElement ToXml()
96                 {
97                         return ToXml (null);
98                 }
99
100                 public SecurityElement ToXml (PolicyLevel level)
101                 {
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);
109                         return se;
110                 }
111
112                 public void FromXml (SecurityElement element)
113                 {
114                         FromXml (element, null);
115                 }
116                 
117                 public void FromXml (SecurityElement e,
118                         PolicyLevel level)
119                 {
120                         if (e == null)
121                                 throw new ArgumentNullException ();
122                         if (e.Tag != XmlTag)
123                                 throw new ArgumentException(
124                                         "e","The Tag of SecurityElement must be " + XmlTag);
125                         
126                         string value = (string)e.Attributes["HashValue"];
127                         string algorithm = (string)e.Attributes["HashAlgorithm"];
128
129                         if (value == null || algorithm == null )
130                                 throw new ArgumentException ();
131                         
132                         hash_value = Encoding.Default.GetBytes (value);
133                         hash_algorithm = (HashAlgorithm)Assembly.GetExecutingAssembly ().CreateInstance (algorithm);
134                         
135                 }
136
137                 [MonoTODO("This is not right")]
138                 public override int GetHashCode ()
139                 {
140                         return hash_value.GetHashCode ();
141                 }
142                 
143                 public override string ToString ()
144                 {
145                         StringBuilder builder = new StringBuilder ();
146                         Type alg_type = hash_algorithm.GetType ();
147
148                         builder.Append ("Hash -");
149                         builder.AppendFormat ("{0} {1}", alg_type.FullName, 
150                                 alg_type.Assembly);
151                         builder.AppendFormat (" = ",  Encoding.Default.GetString (hash_value));
152
153                         return builder.ToString ();
154                 }
155
156                 //
157                 // Private Methods
158                 //
159
160                 private bool EqualsHashValue (byte[] value)
161                 {
162                         int len;
163
164                         if (value.Length != hash_value.Length)
165                                 return false;
166                         
167                         len = value.Length;
168                         for (int i=0; i<len; i++ ) {
169                                 if (value[i] != hash_value[i])
170                                         return false;
171                         }
172
173                         return true;
174                 }
175         }
176 }
177