2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / Test / System.Security.Cryptography / RSAPKCS1SignatureFormatterTest.cs
1 //
2 // RSAPKCS1SignatureFormatterTest.cs - NUnit tests for PKCS#1 v.1.5 signature.
3 //
4 // Author:
5 //      Sebastien Pouliot (sebastien@ximian.com)
6 //
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
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 NUnit.Framework;
31 using System;
32 using System.Security.Cryptography;
33
34 namespace MonoTests.System.Security.Cryptography {
35
36         [TestFixture]
37         public class RSAPKCS1SignatureFormatterTest : Assertion {
38
39 #if NET_2_0
40                 // FX 2.0 changed the OID values for SHA256, SHA384 and SHA512 - so the signature values are different
41                 private static byte[] signatureRSASHA256 = { 0xAD, 0x6E, 0x29, 0xC8, 0x7D, 0xFE, 0x5F, 0xB3, 0x92, 0x07, 0x4C, 0x51, 0x08, 0xC5, 0x91, 0xA2, 0xCF, 0x7E, 0xA6, 0x05, 0x66, 0x85, 0xA3, 0x8E, 0x7C, 0xB0, 0xCA, 0x93, 0x4F, 0x4E, 0xF5, 0x45, 0x0F, 0xED, 0x46, 0xFB, 0x34, 0xBC, 0x8A, 0x6A, 0x48, 0xD9, 0x76, 0x28, 0xE1, 0x68, 0xA0, 0x1F, 0x7F, 0x3E, 0xCC, 0x0A, 0x5F, 0x06, 0x8E, 0xEB, 0xB7, 0xA7, 0x48, 0x6B, 0x92, 0x1A, 0x7A, 0x66, 0x42, 0x4F, 0x0B, 0xC1, 0x19, 0x96, 0xAC, 0x67, 0xA0, 0x6C, 0x3E, 0x39, 0xD2, 0xEB, 0xCA, 0xD7, 0x12, 0x29, 0x46, 0x0A, 0x60, 0x70, 0xA9, 0x2B, 0x80, 0x9F, 0xCD, 0x08, 0x02, 0xEB, 0xA5, 0x62, 0xEC, 0xAB, 0xBB, 0x64, 0x8B, 0x2D, 0xB9, 0x55, 0x0A, 0xE3, 0x5A, 0x2C, 0xDA, 0x54, 0xD4, 0x79, 0x0A, 0x8D, 0xB6, 0x57, 0x05, 0xF7, 0x6C, 0x6D, 0xB7, 0xD8, 0xB4, 0x07, 0xC4, 0xCD, 0x79, 0xD4 };
42                 private static byte[] signatureRSASHA384 = { 0x53, 0x80, 0xFD, 0x26, 0x8F, 0xCF, 0xE5, 0x44, 0x55, 0x4A, 0xC5, 0xB2, 0x46, 0x78, 0x89, 0x42, 0xF8, 0x51, 0xB8, 0x4D, 0x3B, 0xCA, 0x48, 0x5A, 0x36, 0x9F, 0x62, 0x01, 0x72, 0x1E, 0xD8, 0x2D, 0xC2, 0x2D, 0x3E, 0x67, 0x1C, 0x5D, 0x89, 0xAB, 0x39, 0x8D, 0x07, 0xC8, 0xD4, 0x47, 0x97, 0xA4, 0x68, 0x7A, 0x87, 0xA4, 0xCF, 0x7B, 0x32, 0x4F, 0xD3, 0xD1, 0x90, 0xDC, 0x76, 0x23, 0x51, 0xA7, 0xEE, 0xFC, 0x7F, 0xDF, 0x3C, 0xB0, 0x05, 0xF3, 0xE3, 0xAA, 0x96, 0x30, 0xE0, 0xE4, 0x8B, 0x09, 0xB1, 0x78, 0xAC, 0x99, 0xDB, 0xC5, 0x0E, 0xFA, 0xAB, 0x4F, 0xA1, 0x02, 0xCA, 0x77, 0x93, 0x74, 0x5A, 0xB8, 0x71, 0x9C, 0x3E, 0x2E, 0xAE, 0x62, 0xC7, 0xE5, 0xBF, 0xDA, 0xFE, 0x31, 0xA7, 0x91, 0xC0, 0x04, 0xE3, 0x95, 0xCB, 0x3F, 0x54, 0xA8, 0x09, 0x25, 0xF7, 0x09, 0x78, 0xE6, 0x09, 0x84 };
43                 private static byte[] signatureRSASHA512 = { 0xA8, 0xD0, 0x24, 0xCB, 0xA2, 0x4B, 0x5E, 0x0D, 0xBC, 0x3F, 0x6F, 0x0F, 0x8D, 0xE4, 0x31, 0x9E, 0x37, 0x84, 0xE0, 0x31, 0x5B, 0x63, 0x24, 0xC5, 0xA9, 0x05, 0x41, 0xAA, 0x69, 0x02, 0x8F, 0xC1, 0x57, 0x06, 0x1F, 0xBF, 0x3B, 0x8B, 0xC8, 0x86, 0xB3, 0x02, 0xEA, 0xF1, 0x75, 0xE4, 0x70, 0x21, 0x1E, 0x16, 0x4C, 0x37, 0xB2, 0x31, 0x78, 0xD0, 0xA0, 0x88, 0xA5, 0x1D, 0x5D, 0x8F, 0xBC, 0xC3, 0x87, 0x94, 0x4B, 0x8F, 0x4E, 0x92, 0xBC, 0x80, 0xF8, 0xA5, 0x90, 0xF7, 0xA0, 0x6D, 0x96, 0x61, 0x65, 0x0D, 0xD5, 0x3F, 0xD7, 0x4F, 0x07, 0x58, 0x40, 0xB8, 0xA4, 0x14, 0x14, 0x55, 0x39, 0x4F, 0xF0, 0xB5, 0x56, 0x99, 0xC8, 0x52, 0x0C, 0xDD, 0xBA, 0x8D, 0xFB, 0x06, 0x83, 0x6E, 0x79, 0x25, 0x75, 0xEF, 0x0D, 0x26, 0x14, 0x3A, 0xBB, 0x62, 0x29, 0x21, 0xF6, 0x4B, 0x9E, 0x87, 0x28, 0x57 };
44 #else
45                 private static byte[] signatureRSASHA256 = { 0x0F, 0xE3, 0x15, 0x5B, 0x4D, 0xA1, 0xB4, 0x13, 0x93, 0x91, 0x1E, 0x17, 0xF9, 0x36, 0xB3, 0x2C, 0xAC, 0x51, 0x77, 0xBC, 0x86, 0x21, 0xB0, 0x69, 0x75, 0x57, 0xAF, 0xB0, 0xAD, 0xF9, 0x42, 0xF5, 0x58, 0xBC, 0xD5, 0x61, 0xD5, 0x14, 0x8E, 0xC6, 0xE0, 0xB3, 0xB5, 0x51, 0xCD, 0x17, 0x68, 0x58, 0x27, 0x74, 0x8A, 0xA7, 0x88, 0xB9, 0x24, 0xD6, 0xE4, 0xC4, 0x93, 0x82, 0x95, 0xB4, 0x36, 0x14, 0x48, 0xA7, 0xF6, 0x27, 0x87, 0xEB, 0xD8, 0xB9, 0x75, 0x14, 0x75, 0xFB, 0x6E, 0xA1, 0xF7, 0xAB, 0xA6, 0x78, 0x32, 0xEF, 0x1A, 0x23, 0x60, 0xD3, 0x0C, 0x8D, 0xFE, 0x89, 0x72, 0xB7, 0x93, 0x6D, 0x00, 0x25, 0xED, 0xF5, 0x55, 0x66, 0xA8, 0x52, 0x7F, 0x20, 0xFD, 0x77, 0xDA, 0x10, 0x77, 0xE9, 0xF0, 0x58, 0x8D, 0xE6, 0x3A, 0x5A, 0x00, 0x83, 0x64, 0x42, 0xA5, 0x15, 0x79, 0x3C, 0xB0, 0x8F };
46                 private static byte[] signatureRSASHA384 = { 0x86, 0x20, 0x2A, 0xB6, 0xA8, 0x0F, 0x59, 0x42, 0xCA, 0x83, 0xC3, 0x46, 0x2C, 0xA9, 0x2E, 0x62, 0x73, 0x2C, 0xEE, 0x52, 0xA5, 0xAE, 0x4F, 0xFD, 0xB1, 0x1F, 0xFA, 0x0C, 0x71, 0x4A, 0xFD, 0xE2, 0xAC, 0x64, 0x1C, 0x63, 0x41, 0xB8, 0x43, 0x3F, 0x8A, 0xF3, 0x7E, 0x1C, 0x25, 0xBE, 0xEE, 0xFC, 0x7C, 0xCB, 0x33, 0x72, 0x3B, 0x91, 0x1F, 0xF3, 0x78, 0xC2, 0xD0, 0xEA, 0xDF, 0x69, 0xE9, 0x31, 0x2F, 0x39, 0x32, 0x5F, 0x4A, 0x51, 0xAE, 0x24, 0x9E, 0x96, 0x77, 0xFB, 0x16, 0xC4, 0xDD, 0x98, 0xDA, 0xA9, 0x9D, 0xA0, 0x7C, 0x2C, 0x95, 0x12, 0x53, 0x1F, 0x7B, 0x23, 0xEE, 0x78, 0x95, 0x57, 0xFF, 0x02, 0x57, 0x2B, 0x4A, 0x3E, 0x62, 0x6A, 0xC0, 0x99, 0xDF, 0x4B, 0x7E, 0xBF, 0x86, 0xC4, 0xFB, 0x8E, 0xF3, 0x70, 0xA2, 0xEE, 0x7B, 0xCA, 0x8B, 0x22, 0xA4, 0x07, 0xBA, 0xBD, 0x16, 0xA9 };
47                 private static byte[] signatureRSASHA512 = { 0xB7, 0x7E, 0x7E, 0xEF, 0x95, 0xCE, 0xE8, 0x9D, 0x0F, 0x40, 0x35, 0x50, 0x88, 0xFE, 0x8B, 0xA3, 0x26, 0xD3, 0x9E, 0xA7, 0x82, 0x23, 0x1A, 0x46, 0x13, 0x46, 0x81, 0x59, 0xD1, 0x24, 0x45, 0xAC, 0x53, 0xEF, 0x5A, 0x06, 0x31, 0xA7, 0xC2, 0x76, 0xDC, 0x2B, 0x60, 0x69, 0xB1, 0x36, 0x1D, 0xE1, 0xFC, 0xD5, 0x9A, 0x01, 0x71, 0x08, 0xE9, 0x0C, 0xAE, 0xF4, 0x29, 0xCF, 0xC4, 0xB0, 0x60, 0xA4, 0xBE, 0x1C, 0x9B, 0x05, 0x2A, 0xA9, 0x6A, 0x12, 0xFF, 0x73, 0x84, 0x5C, 0xA8, 0x74, 0x5B, 0x9C, 0xA2, 0x07, 0x9D, 0x73, 0xB8, 0xE3, 0x20, 0x16, 0x3C, 0x47, 0x8F, 0x27, 0x7A, 0x48, 0xAF, 0x01, 0x07, 0xA0, 0x6A, 0x2D, 0x71, 0xAD, 0xDD, 0x8B, 0x68, 0xC8, 0x32, 0x61, 0x95, 0x68, 0x22, 0x1B, 0x8B, 0xD9, 0x86, 0xA7, 0xBE, 0x60, 0x06, 0x70, 0x7C, 0xED, 0x51, 0x28, 0x66, 0x28, 0xF0, 0x65 };
48 #endif
49                 private static RSA rsa;
50                 private static DSA dsa;
51
52                 private RSAPKCS1SignatureFormatter fmt;
53
54                 [SetUp]
55                 public void SetUp () 
56                 {
57                         if (rsa == null) {
58                                 rsa = RSA.Create ();
59                                 rsa.ImportParameters (AllTests.GetRsaKey (true));
60                         }
61                         if (dsa == null)
62                                 dsa = DSA.Create ();
63                 }
64
65                 public void AssertEquals (string msg, byte[] array1, byte[] array2) 
66                 {
67                         AllTests.AssertEquals (msg, array1, array2);
68                 }
69
70                 [Test]
71                 public void ConstructorEmpty () 
72                 {
73                         fmt = new RSAPKCS1SignatureFormatter ();
74                         AssertNotNull ("RSAPKCS1SignatureFormatter()", fmt);
75                 }
76
77                 [Test]
78 #if NET_2_0
79                 [ExpectedException (typeof (ArgumentNullException))]
80 #endif
81                 public void ConstructorNull () 
82                 {
83                         fmt = new RSAPKCS1SignatureFormatter (null);
84                         AssertNotNull ("RSAPKCS1SignatureFormatter(null)", fmt);
85                 }
86
87                 [Test]
88                 public void ConstructorRSA () 
89                 {
90                         fmt = new RSAPKCS1SignatureFormatter (rsa);
91                         AssertNotNull ("RSAPKCS1SignatureFormatter(rsa)", fmt);
92                 }
93
94                 [Test]
95                 [ExpectedException (typeof (InvalidCastException))]
96                 public void ConstructorDSA () 
97                 {
98                         fmt = new RSAPKCS1SignatureFormatter (dsa);
99                 }
100
101                 [Test]
102                 public void SetKeyRSA () 
103                 {
104                         fmt = new RSAPKCS1SignatureFormatter ();
105                         fmt.SetKey (rsa);
106                 }
107
108                 [Test]
109                 [ExpectedException (typeof (InvalidCastException))]
110                 public void SetKeyDSA () 
111                 {
112                         fmt = new RSAPKCS1SignatureFormatter ();
113                         fmt.SetKey (dsa);
114                 }
115
116                 [Test]
117 #if NET_2_0
118                 [ExpectedException (typeof (ArgumentNullException))]
119 #endif
120                 public void SetKeyNull ()
121                 {
122                         fmt = new RSAPKCS1SignatureFormatter ();
123                         fmt.SetKey (null);
124                 }
125
126                 [Test]
127                 public void SetHashAlgorithmSHA1 () 
128                 {
129                         fmt = new RSAPKCS1SignatureFormatter ();
130                         fmt.SetHashAlgorithm ("SHA1");
131                 }
132
133                 [Test]
134                 public void SetHashAlgorithmMD5 () 
135                 {
136                         fmt = new RSAPKCS1SignatureFormatter ();
137                         fmt.SetHashAlgorithm ("MD5");
138                 }
139
140                 [Test]
141                 public void SetHashAlgorithmSHA256 () 
142                 {
143                         fmt = new RSAPKCS1SignatureFormatter ();
144                         fmt.SetHashAlgorithm ("SHA256");
145                 }
146
147                 [Test]
148                 public void SetHashAlgorithmSHA384 () 
149                 {
150                         fmt = new RSAPKCS1SignatureFormatter ();
151                         fmt.SetHashAlgorithm ("SHA384");
152                 }
153
154                 [Test]
155                 public void SetHashAlgorithmSHA512 () 
156                 {
157                         fmt = new RSAPKCS1SignatureFormatter ();
158                         fmt.SetHashAlgorithm ("SHA512");
159                 }
160
161                 [Test]
162                 [ExpectedException (typeof (ArgumentNullException))]
163                 public void SetHashAlgorithmNull () 
164                 {
165                         fmt = new RSAPKCS1SignatureFormatter ();
166                         fmt.SetHashAlgorithm (null);
167                 }
168
169                 // see: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcongeneratingsignatures.asp
170
171                 [Test]
172                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
173                 public void CreateSignatureNullHash () 
174                 {
175                         fmt = new RSAPKCS1SignatureFormatter ();
176                         fmt.SetKey (rsa);
177                         byte[] hash = null;
178                         byte[] signature = fmt.CreateSignature (hash);
179                 }
180
181                 [Test]
182                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
183                 public void CreateSignatureNoHashAlgorithm () 
184                 {
185                         fmt = new RSAPKCS1SignatureFormatter ();
186                         // no hash algorithm
187                         byte[] hash = new byte [1];
188                         byte[] signature = fmt.CreateSignature (hash);
189                 }
190
191                 [Test]
192                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
193                 public void CreateSignatureNoKey () 
194                 {
195                         fmt = new RSAPKCS1SignatureFormatter ();
196                         // no key
197                         fmt.SetHashAlgorithm ("SHA1");
198                         byte[] hash = new byte [20];
199                         byte[] signature = fmt.CreateSignature (hash);
200                 }
201
202                 [Test]
203                 public void CreateSignatureRSASHA1 () 
204                 {
205                         fmt = new RSAPKCS1SignatureFormatter ();
206                         // we need the private key 
207                         fmt.SetKey (rsa);
208                         // good SHA1
209                         fmt.SetHashAlgorithm ("SHA1");
210                         byte[] hash = new byte [20];
211                         byte[] signature = fmt.CreateSignature (hash);
212                         AssertNotNull ("CreateSignature(SHA1)", signature);
213                 }
214
215                 [Test]
216                 [ExpectedException (typeof (CryptographicException))]
217                 public void CreateSignatureRSASHA1BadLength () 
218                 {
219                         fmt = new RSAPKCS1SignatureFormatter ();
220                         // we need the private key 
221                         fmt.SetKey (rsa);
222                         // wrong length SHA1
223                         fmt.SetHashAlgorithm ("SHA1");
224                         byte[] hash = new byte [19];
225                         byte[] signature = fmt.CreateSignature (hash);
226                 }
227
228                 [Test]
229                 public void CreateSignatureRSAMD5 () 
230                 {
231                         fmt = new RSAPKCS1SignatureFormatter ();
232                         // we need the private key 
233                         fmt.SetKey (rsa);
234                         // good MD5
235                         fmt.SetHashAlgorithm ("MD5");
236                         byte[] hash = new byte [16];
237                         byte[] signature = fmt.CreateSignature (hash);
238                         AssertNotNull ("CreateSignature(MD5)", signature);
239                 }
240
241                 private byte[] CreateSignature (string hashName, int hashSize) 
242                 {
243                         byte[] signature = null;
244                         fmt = new RSAPKCS1SignatureFormatter ();
245                         bool ms = false;
246                         // we need the private key 
247                         RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
248                         if (rsa == null) {
249                                 ms = true;
250                                 rsa = RSA.Create ();
251                         }
252                         rsa.ImportParameters (AllTests.GetRsaKey (true));
253                         fmt.SetKey (rsa);
254                         try {
255                                 HashAlgorithm ha = HashAlgorithm.Create (hashName);
256                                 byte[] data = new byte [ha.HashSize >> 3];
257                                 // this way we get the same results as CreateSignatureHash
258                                 data = ha.ComputeHash (data);
259
260                                 fmt.SetHashAlgorithm (hashName);
261                                 signature = fmt.CreateSignature (data);
262                                 if (ms)
263                                         Fail ("CreateSignature(" + hashName + ") Expected CryptographicException but got none");
264                         }
265                         catch (CryptographicException) {
266                                 if (!ms)
267                                         throw;
268                         }
269                         catch (Exception e) {
270                                 if (ms)
271                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got " + e.ToString ());
272                         }
273                         return signature;
274                 }
275
276                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
277                 // supported by Mono::
278                 [Test]
279                 public void CreateSignatureRSASHA256 () 
280                 {
281                         byte[] signature = CreateSignature ("SHA256", 32);
282                         if (signature != null)
283                                 AssertEquals ("CreateSignature(SHA256)", signatureRSASHA256, signature);
284                 }
285
286                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
287                 // supported by Mono::
288                 [Test]
289                 public void CreateSignatureRSASHA384 () 
290                 {
291                         byte[] signature = CreateSignature ("SHA384", 48);
292                         if (signature != null)
293                                 AssertEquals ("CreateSignature(SHA384)", signatureRSASHA384, signature);
294                 }
295
296                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
297                 // supported by Mono::
298                 [Test]
299                 public void CreateSignatureRSASHA512 () 
300                 {
301                         byte[] signature = CreateSignature ("SHA512", 64);
302                         if (signature != null)
303                                 AssertEquals ("CreateSignature(SHA512)", signatureRSASHA512, signature);
304                 }
305
306                 [Test]
307                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
308                 public void CreateSignatureRSABadHash () 
309                 {
310                         fmt = new RSAPKCS1SignatureFormatter ();
311                         // we need the private key 
312                         fmt.SetKey (rsa);
313                         // null (bad ;-)
314                         byte[] hash = null;
315                         byte[] signature  = fmt.CreateSignature (hash);
316                 }
317
318                 [Test]
319                 [ExpectedException (typeof (ArgumentNullException))]
320                 public void CreateSignatureHashBadHash () 
321                 {
322                         fmt = new RSAPKCS1SignatureFormatter ();
323                         HashAlgorithm hash = null;
324                         byte[] data = new byte [20];
325                         // no hash algorithm
326                         byte[] signature = fmt.CreateSignature (hash);
327                 }
328
329                 [Test]
330                 [ExpectedException (typeof (CryptographicUnexpectedOperationException))]
331                 public void CreateSignatureHashNoKey () 
332                 {
333                         fmt = new RSAPKCS1SignatureFormatter ();
334                         byte[] data = new byte [20];
335                         // no key
336                         HashAlgorithm hash = SHA1.Create ();
337                         hash.ComputeHash (data);
338                         byte[] signature = fmt.CreateSignature (hash);
339                 }
340
341                 [Test]
342                 public void CreateSignatureHashSHA1 () 
343                 {
344                         fmt = new RSAPKCS1SignatureFormatter ();
345                         byte[] data = new byte [20];
346                         // we need the private key 
347                         fmt.SetKey (rsa);
348                         // good SHA1
349                         HashAlgorithm hash = SHA1.Create ();
350                         hash.ComputeHash (data);
351                         byte[] signature = fmt.CreateSignature (hash);
352                         AssertNotNull ("CreateSignature(SHA1)", signature);
353                 }
354
355                 [Test]
356                 public void CreateSignatureHashMD5 () 
357                 {
358                         fmt = new RSAPKCS1SignatureFormatter ();
359                         byte[] data = new byte [16];
360                         // we need the private key 
361                         fmt.SetKey (rsa);
362                         // good MD5
363                         HashAlgorithm hash = MD5.Create ();
364                         hash.ComputeHash (data);
365                         byte[] signature = fmt.CreateSignature (hash);
366                         AssertNotNull ("CreateSignature(MD5)", signature);
367                 }
368
369                 private byte[] CreateSignatureHash (string hashName) 
370                 {
371                         byte[] signature = null;
372                         fmt = new RSAPKCS1SignatureFormatter ();
373                         bool ms = false;
374                         // we need the private key 
375                         RSA rsa = RSA.Create ("Mono.Security.Cryptography.RSAManaged"); // only available with Mono::
376                         if (rsa == null) {
377                                 ms = true;
378                                 rsa = RSA.Create ();
379                         }
380                         rsa.ImportParameters (AllTests.GetRsaKey (true));
381                         fmt.SetKey (rsa);
382                         try {
383                                 HashAlgorithm hash = HashAlgorithm.Create (hashName);
384                                 byte[] data = new byte [(hash.HashSize >> 3)];
385                                 hash.ComputeHash (data);
386                                 signature = fmt.CreateSignature (hash);
387                                 if (ms)
388                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got none");
389                         }
390                         catch (CryptographicException) {
391                                 if (!ms)
392                                         throw;
393                         }
394                         catch (Exception e) {
395                                 if (ms)
396                                         Fail ("CreateSignatureHash(" + hashName + ") Expected CryptographicException but got " + e.ToString ());
397                         }
398                         return signature;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     
399                 }
400
401                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
402                 // supported by Mono::
403                 [Test]
404                 public void CreateSignatureHashSHA256 () 
405                 {
406                         byte[] signature = CreateSignatureHash ("SHA256");
407                         if (signature != null)
408                                 AssertEquals ("CreateSignatureHash(SHA256)", signatureRSASHA256, signature);
409                 }
410
411                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
412                 // supported by Mono::
413                 [Test]
414                 public void CreateSignatureHashSHA384 () 
415                 {
416                         byte[] signature = CreateSignatureHash ("SHA384");
417                         if (signature != null)
418                                 AssertEquals ("CreateSignatureHash(SHA384)", signatureRSASHA384, signature);
419                 }
420
421                 // not supported using MS framework 1.0 and 1.1 (CryptographicException)
422                 // supported by Mono::
423                 [Test]
424                 public void CreateSignatureHashSHA512 () 
425                 {
426                         byte[] signature = CreateSignatureHash ("SHA512");
427                         if (signature != null)
428                                 AssertEquals ("CreateSignatureHash(SHA512)", signatureRSASHA512, signature);
429                 }
430         }
431 }