Add license and copyright to all source files in corlib
[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 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 using System.Text;
34 using System.Reflection;
35 using System.Security.Cryptography;
36
37 namespace System.Security.Policy {
38
39         [Serializable]
40         public sealed class HashMembershipCondition : IMembershipCondition, 
41                 ISecurityEncodable, ISecurityPolicyEncodable {
42
43                 private static readonly string XmlTag = "IMembershipCondition";
44
45                 private HashAlgorithm hash_algorithm;
46                 private byte[] hash_value;
47
48                 public HashMembershipCondition (HashAlgorithm hash_algorithm,
49                         byte[] hash_value)
50                 {
51                         if (hash_algorithm == null || hash_value == null)
52                                 throw new ArgumentNullException ();
53                                 
54                         this.hash_algorithm = hash_algorithm;
55                         this.hash_value = hash_value;
56                 }
57
58                 //
59                 // Public Properties
60                 //
61                 
62                 public HashAlgorithm HashAlgorithm {
63                         get { return hash_algorithm; }
64                         set { 
65                                 if (value == null)
66                                         throw new ArgumentNullException ();
67                                 hash_algorithm = value; 
68                         }
69                 }
70
71                 public byte[] HashValue {
72                         get { return hash_value; }
73                         set { 
74                                 if (value == null)
75                                         throw new ArgumentNullException ();
76                                 hash_value = value; 
77                         } 
78                 }
79
80                 //
81                 // Public Methods
82                 //
83
84                 public bool Check (Evidence evidence)
85                 {
86                         if (evidence == null)
87                                 throw new ArgumentNullException ();
88
89                         // Loop through evidence finding the first Hash object
90                         foreach (object obj in evidence) {
91                                 Hash hash = obj as Hash;
92                                 if (hash == null)
93                                         continue;
94                                 if (EqualsHashValue (hash.GenerateHash (hash_algorithm)))
95                                         return true;
96                                 break;
97                         }
98                         return false;
99                 }
100
101                 public IMembershipCondition Copy ()
102                 {
103                         return new HashMembershipCondition (hash_algorithm, hash_value);
104                 }
105
106                 public override bool Equals (object o)
107                 {
108                         HashMembershipCondition other;
109                         if (!(o is HashMembershipCondition))
110                                 return false;
111
112                         other = (HashMembershipCondition)o;
113                         
114                         return (other.HashAlgorithm == hash_algorithm &&
115                                 other.HashValue == hash_value);
116                 }
117                 
118                 public SecurityElement ToXml()
119                 {
120                         return ToXml (null);
121                 }
122
123                 public SecurityElement ToXml (PolicyLevel level)
124                 {
125                         SecurityElement se = new SecurityElement (XmlTag);
126                         Type type = this.GetType ();
127                         string classString = type.FullName + ", " + type.Assembly;
128                         se.AddAttribute ("class", classString);
129                         se.AddAttribute ("version", "1");
130                         se.AddAttribute ("HashValue", Encoding.Default.GetString (hash_value));
131                         se.AddAttribute ("HashAlgorithm", hash_algorithm.GetType ().FullName);
132                         return se;
133                 }
134
135                 public void FromXml (SecurityElement element)
136                 {
137                         FromXml (element, null);
138                 }
139                 
140                 public void FromXml (SecurityElement e,
141                         PolicyLevel level)
142                 {
143                         if (e == null)
144                                 throw new ArgumentNullException ();
145                         if (e.Tag != XmlTag)
146                                 throw new ArgumentException(
147                                         "e","The Tag of SecurityElement must be " + XmlTag);
148                         
149                         string value = (string)e.Attributes["HashValue"];
150                         string algorithm = (string)e.Attributes["HashAlgorithm"];
151
152                         if (value == null || algorithm == null )
153                                 throw new ArgumentException ();
154                         
155                         hash_value = Encoding.Default.GetBytes (value);
156                         hash_algorithm = (HashAlgorithm)Assembly.GetExecutingAssembly ().CreateInstance (algorithm);
157                         
158                 }
159
160                 [MonoTODO("This is not right")]
161                 public override int GetHashCode ()
162                 {
163                         return hash_value.GetHashCode ();
164                 }
165                 
166                 public override string ToString ()
167                 {
168                         StringBuilder builder = new StringBuilder ();
169                         Type alg_type = hash_algorithm.GetType ();
170
171                         builder.Append ("Hash -");
172                         builder.AppendFormat ("{0} {1}", alg_type.FullName, 
173                                 alg_type.Assembly);
174                         builder.AppendFormat (" = ",  Encoding.Default.GetString (hash_value));
175
176                         return builder.ToString ();
177                 }
178
179                 //
180                 // Private Methods
181                 //
182
183                 private bool EqualsHashValue (byte[] value)
184                 {
185                         int len;
186
187                         if (value.Length != hash_value.Length)
188                                 return false;
189                         
190                         len = value.Length;
191                         for (int i=0; i<len; i++ ) {
192                                 if (value[i] != hash_value[i])
193                                         return false;
194                         }
195
196                         return true;
197                 }
198         }
199 }
200