2009-09-03 Jb Evain <jbevain@novell.com>
[mono.git] / mcs / class / System.Core / System.Security.Cryptography / CngAlgorithm.cs
1 //
2 // System.Security.Cryptography.CngAlgorithm
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30
31 namespace System.Security.Cryptography {
32
33         // note: CNG stands for "Cryptography API: Next Generation"
34
35         [Serializable]
36         public sealed class CngAlgorithm : IEquatable<CngAlgorithm> {
37
38                 private string algo;
39
40                 public CngAlgorithm (string algorithm)
41                 {
42                         if (algorithm == null)
43                                 throw new ArgumentNullException ("algorithm");
44                         if (algorithm.Length == 0)
45                                 throw new ArgumentException ("algorithm");
46
47                         algo = algorithm;
48                 }
49
50                 public string Algorithm {
51                         get { return algo; }
52                 }
53
54                 public bool Equals (CngAlgorithm other)
55                 {
56                         if (other == null)
57                                 return false;
58                         return algo == other.algo;
59                 }
60
61                 public override bool Equals (object obj)
62                 {
63                         return Equals (obj as CngAlgorithm);
64                 }
65
66                 public override int GetHashCode ()
67                 {
68                         return algo.GetHashCode ();
69                 }
70
71                 public override string ToString ()
72                 {
73                         return algo;
74                 }
75
76                 // static
77
78                 static CngAlgorithm dh256;
79                 static CngAlgorithm dh384;
80                 static CngAlgorithm dh521;
81                 static CngAlgorithm dsa256;
82                 static CngAlgorithm dsa384;
83                 static CngAlgorithm dsa521;
84                 static CngAlgorithm md5;
85                 static CngAlgorithm sha1;
86                 static CngAlgorithm sha256;
87                 static CngAlgorithm sha384;
88                 static CngAlgorithm sha512;
89
90                 public static CngAlgorithm ECDiffieHellmanP256 {
91                         get {
92                                 if (dh256 == null)
93                                         dh256 = new CngAlgorithm ("ECDH_P256");
94                                 return dh256;
95                         }
96                 }
97
98                 public static CngAlgorithm ECDiffieHellmanP384 {
99                         get {
100                                 if (dh384 == null)
101                                         dh384 = new CngAlgorithm ("ECDH_P384");
102                                 return dh384;
103                         }
104                 }
105
106                 public static CngAlgorithm ECDiffieHellmanP521 {
107                         get {
108                                 if (dh521 == null)
109                                         dh521 = new CngAlgorithm ("ECDH_P521");
110                                 return dh521;
111                         }
112                 }
113
114                 public static CngAlgorithm ECDsaP256 {
115                         get {
116                                 if (dsa256 == null)
117                                         dsa256 = new CngAlgorithm ("ECDSA_P256");
118                                 return dsa256;
119                         }
120                 }
121
122                 public static CngAlgorithm ECDsaP384 {
123                         get {
124                                 if (dsa384 == null)
125                                         dsa384 = new CngAlgorithm ("ECDSA_P384");
126                                 return dsa384;
127                         }
128                 }
129
130                 public static CngAlgorithm ECDsaP521 {
131                         get {
132                                 if (dsa521 == null)
133                                         dsa521 = new CngAlgorithm ("ECDSA_P521");
134                                 return dsa521;
135                         }
136                 }
137
138                 public static CngAlgorithm MD5 {
139                         get {
140                                 if (md5 == null)
141                                         md5 = new CngAlgorithm ("MD5");
142                                 return md5;
143                         }
144                 }
145
146                 public static CngAlgorithm Sha1 {
147                         get {
148                                 if (sha1 == null)
149                                         sha1 = new CngAlgorithm ("SHA1");
150                                 return sha1;
151                         }
152                 }
153
154                 public static CngAlgorithm Sha256 {
155                         get {
156                                 if (sha256 == null)
157                                         sha256 = new CngAlgorithm ("SHA256");
158                                 return sha256;
159                         }
160                 }
161
162                 public static CngAlgorithm Sha384 {
163                         get {
164                                 if (sha384 == null)
165                                         sha384 = new CngAlgorithm ("SHA384");
166                                 return sha384;
167                         }
168                 }
169
170                 public static CngAlgorithm Sha512 {
171                         get {
172                                 if (sha512 == null)
173                                         sha512 = new CngAlgorithm ("SHA512");
174                                 return sha512;
175                         }
176                 }
177
178                 public static bool operator == (CngAlgorithm left, CngAlgorithm right)
179                 {
180                         if ((object)left == null)
181                                 return ((object)right == null);
182                         if ((object)right == null)
183                                 return false;
184                         return left.algo == right.algo;
185                 }
186
187                 public static bool operator != (CngAlgorithm left, CngAlgorithm right)
188                 {
189                         if ((object)left == null)
190                                 return ((object)right != null);
191                         if ((object)right == null)
192                                 return true;
193                         return left.algo != right.algo;
194                 }
195         }
196 }