Merge pull request #2576 from ludovic-henry/referencesource-process
[mono.git] / mcs / class / System.Core / System.Security.Cryptography / CngKeyBlobFormat.cs
1 //
2 // System.Security.Cryptography.CngKeyBlobFormat
3 //
4 // Authors:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2008 Novell, Inc (http://www.novell.com)
8 // Copyright (C) 2011 Juho Vähä-Herttua
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System;
31
32 namespace System.Security.Cryptography {
33
34         // note: CNG stands for "Cryptography API: Next Generation"
35
36         [Serializable]
37         public sealed class CngKeyBlobFormat : IEquatable<CngKeyBlobFormat> {
38
39                 private string m_format;
40
41                 public CngKeyBlobFormat (string format)
42                 {
43                         if (format == null)
44                                 throw new ArgumentNullException ("format");
45                         if (format.Length == 0)
46                                 throw new ArgumentException ("format");
47
48                         m_format = format;
49                 }
50
51                 public string Format {
52                         get { return m_format; }
53                 }
54
55                 public bool Equals (CngKeyBlobFormat other)
56                 {
57                         if (other == null)
58                                 return false;
59                         return m_format == other.m_format;
60                 }
61
62                 public override bool Equals (object obj)
63                 {
64                         return Equals (obj as CngKeyBlobFormat);
65                 }
66
67                 public override int GetHashCode ()
68                 {
69                         return m_format.GetHashCode ();
70                 }
71
72                 public override string ToString ()
73                 {
74                         return m_format;
75                 }
76
77                 // static
78
79                 private static CngKeyBlobFormat opaqueTransportBlob;
80                 private static CngKeyBlobFormat genericPrivateBlob;
81                 private static CngKeyBlobFormat genericPublicBlob;
82                 private static CngKeyBlobFormat eccPrivateBlob;
83                 private static CngKeyBlobFormat eccPublicBlob;
84                 private static CngKeyBlobFormat pkcs8PrivateBlob;
85
86                 public static CngKeyBlobFormat OpaqueTransportBlob {
87                         get {
88                                 if (opaqueTransportBlob == null)
89                                         opaqueTransportBlob = new CngKeyBlobFormat ("OpaqueTransport");
90                                 return opaqueTransportBlob;
91                         }
92                 }
93
94                 public static CngKeyBlobFormat GenericPrivateBlob {
95                         get {
96                                 if (genericPrivateBlob == null)
97                                         genericPrivateBlob = new CngKeyBlobFormat ("PRIVATEBLOB");
98                                 return genericPrivateBlob;
99                         }
100                 }
101
102                 public static CngKeyBlobFormat GenericPublicBlob {
103                         get {
104                                 if (genericPublicBlob == null)
105                                         genericPublicBlob = new CngKeyBlobFormat ("PUBLICBLOB");
106                                 return genericPublicBlob;
107                         }
108                 }
109
110                 public static CngKeyBlobFormat EccPrivateBlob {
111                         get {
112                                 if (eccPrivateBlob == null)
113                                         eccPrivateBlob = new CngKeyBlobFormat ("ECCPRIVATEBLOB");
114                                 return eccPrivateBlob;
115                         }
116                 }
117
118                 public static CngKeyBlobFormat EccPublicBlob {
119                         get {
120                                 if (eccPublicBlob == null)
121                                         eccPublicBlob = new CngKeyBlobFormat ("ECCPUBLICBLOB");
122                                 return eccPublicBlob;
123                         }
124                 }
125
126                 public static CngKeyBlobFormat Pkcs8PrivateBlob {
127                         get {
128                                 if (pkcs8PrivateBlob == null)
129                                         pkcs8PrivateBlob = new CngKeyBlobFormat ("PKCS8_PRIVATEKEY");
130                                 return pkcs8PrivateBlob;
131                         }
132                 }
133
134                 public static bool operator == (CngKeyBlobFormat left, CngKeyBlobFormat right)
135                 {
136                         if ((object)left == null)
137                                 return ((object)right == null);
138                         return left.Equals (right);
139                 }
140
141                 public static bool operator != (CngKeyBlobFormat left, CngKeyBlobFormat right)
142                 {
143                         if ((object)left == null)
144                                 return ((object)right != null);
145                         return !left.Equals (right);
146                 }
147         }
148 }