Add ClientDisconnectedToken property to HttpResponseBase and Wrapper as default None
[mono.git] / mcs / class / System.Core / System.Security.Cryptography / CngProperty.cs
1 //
2 // System.Security.Cryptography.CngProperty
3 //
4 // Copyright (C) 2011 Juho Vähä-Herttua
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining
7 // a copy of this software and associated documentation files (the
8 // "Software"), to deal in the Software without restriction, including
9 // without limitation the rights to use, copy, modify, merge, publish,
10 // distribute, sublicense, and/or sell copies of the Software, and to
11 // permit persons to whom the Software is furnished to do so, subject to
12 // the following conditions:
13 // 
14 // The above copyright notice and this permission notice shall be
15 // included in all copies or substantial portions of the Software.
16 // 
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 //
25
26 using System;
27
28 namespace System.Security.Cryptography {
29
30         // note: CNG stands for "Cryptography API: Next Generation"
31
32         public struct CngProperty : IEquatable<CngProperty> {
33                 private string name;
34                 private byte[] val;
35                 private CngPropertyOptions opts;
36
37                 public CngProperty(string name, byte[] value, CngPropertyOptions options)
38                 {
39                         if (name == null)
40                                 throw new ArgumentNullException("name");
41
42                         this.name = name;
43                         this.val = (value != null) ? (byte[]) value.Clone () : null;
44                         this.opts = options;
45                 }
46
47                 public string Name {
48                         get { return name; }
49                 }
50
51                 public CngPropertyOptions Options {
52                         get { return opts; }
53                 }
54
55                 public byte[] GetValue ()
56                 {
57                         if (val == null)
58                                 return null;
59                         return (byte[]) val.Clone ();
60                 }
61
62                 public bool Equals (CngProperty other)
63                 {
64                         if (this.name != other.name || this.opts != other.opts)
65                                 return false;
66                         if (this.val == null && other.val == null)
67                                 return true;
68                         if (this.val == null || other.val == null)
69                                 return false;
70                         if (this.val.Length != other.val.Length)
71                                 return false;
72
73                         for (int i=0; i<val.Length; i++) {
74                                 if (this.val[i] != other.val[i])
75                                         return false;
76                         }
77                         return true;
78                 }
79
80                 public override bool Equals (object obj)
81                 {
82                         if (obj == null)
83                                 return false;
84                         if (!(obj is CngProperty))
85                                 return false;
86                         return Equals ((CngProperty) obj);
87                 }
88
89                 public override int GetHashCode ()
90                 {
91                         int ret = name.GetHashCode () ^ opts.GetHashCode ();
92                         if (val == null)
93                                 return ret;
94
95                         for (int i=0; i<val.Length; i++) {
96                                 // Handle each 4 bytes of byte array as a little-endian
97                                 // integer and XOR it with the resulting hash code value
98                                 ret ^= val[i] << 8*(i % 4);
99                         }
100                         return ret;
101                 }
102
103                 // static
104
105                 public static bool operator == (CngProperty left, CngProperty right)
106                 {
107                         return left.Equals (right);
108                 }
109
110                 public static bool operator != (CngProperty left, CngProperty right)
111                 {
112                         return !left.Equals (right);
113                 }
114         }
115 }