1
1
// Copyright (c) Microsoft. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
- using System . Collections ;
5
4
using System . Collections . Generic ;
6
5
using System . IO ;
7
6
using Test . IO . Streams ;
@@ -147,10 +146,10 @@ public static void ExpectSignature_SHA256_1024_Stream()
147
146
byte [ ] signature ;
148
147
149
148
using ( Stream stream = new PositionValueStream ( 10 ) )
150
- using ( var rsa = new RSACryptoServiceProvider ( ) )
149
+ using ( RSA rsa = RSAFactory . Create ( ) )
151
150
{
152
151
rsa . ImportParameters ( TestData . RSA1024Params ) ;
153
- signature = rsa . SignData ( stream , " SHA256" ) ;
152
+ signature = rsa . SignData ( stream , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
154
153
}
155
154
156
155
Assert . Equal ( expectedSignature , signature ) ;
@@ -287,11 +286,11 @@ public static void SignAndVerify_SHA256_1024()
287
286
[ Fact ]
288
287
public static void NegativeVerify_WrongAlgorithm ( )
289
288
{
290
- using ( var rsa = new RSACryptoServiceProvider ( ) )
289
+ using ( RSA rsa = RSAFactory . Create ( ) )
291
290
{
292
291
rsa . ImportParameters ( TestData . RSA2048Params ) ;
293
- byte [ ] signature = rsa . SignData ( TestData . HelloBytes , " SHA1" ) ;
294
- bool signatureMatched = rsa . VerifyData ( TestData . HelloBytes , " SHA256" , signature ) ;
292
+ byte [ ] signature = rsa . SignData ( TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
293
+ bool signatureMatched = rsa . VerifyData ( TestData . HelloBytes , signature , HashAlgorithmName . SHA256 , RSASignaturePadding . Pkcs1 ) ;
295
294
296
295
Assert . False ( signatureMatched ) ;
297
296
}
@@ -300,27 +299,27 @@ public static void NegativeVerify_WrongAlgorithm()
300
299
[ Fact ]
301
300
public static void NegativeVerify_WrongSignature ( )
302
301
{
303
- using ( var rsa = new RSACryptoServiceProvider ( ) )
302
+ using ( RSA rsa = RSAFactory . Create ( ) )
304
303
{
305
304
rsa . ImportParameters ( TestData . RSA2048Params ) ;
306
- byte [ ] signature = rsa . SignData ( TestData . HelloBytes , " SHA1" ) ;
305
+ byte [ ] signature = rsa . SignData ( TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
307
306
308
307
// Invalidate the signature.
309
308
signature [ 0 ] = ( byte ) ~ signature [ 0 ] ;
310
309
311
- bool signatureMatched = rsa . VerifyData ( TestData . HelloBytes , " SHA1" , signature ) ;
310
+ bool signatureMatched = rsa . VerifyData ( TestData . HelloBytes , signature , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
312
311
Assert . False ( signatureMatched ) ;
313
312
}
314
313
}
315
314
316
315
[ Fact ]
317
316
public static void NegativeVerify_TamperedData ( )
318
317
{
319
- using ( var rsa = new RSACryptoServiceProvider ( ) )
318
+ using ( RSA rsa = RSAFactory . Create ( ) )
320
319
{
321
320
rsa . ImportParameters ( TestData . RSA2048Params ) ;
322
- byte [ ] signature = rsa . SignData ( TestData . HelloBytes , " SHA1" ) ;
323
- bool signatureMatched = rsa . VerifyData ( Array . Empty < byte > ( ) , " SHA1" , signature ) ;
321
+ byte [ ] signature = rsa . SignData ( TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
322
+ bool signatureMatched = rsa . VerifyData ( Array . Empty < byte > ( ) , signature , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
324
323
Assert . False ( signatureMatched ) ;
325
324
}
326
325
}
@@ -330,16 +329,16 @@ public static void NegativeVerify_BadKeysize()
330
329
{
331
330
byte [ ] signature ;
332
331
333
- using ( var rsa = new RSACryptoServiceProvider ( ) )
332
+ using ( RSA rsa = RSAFactory . Create ( ) )
334
333
{
335
334
rsa . ImportParameters ( TestData . RSA2048Params ) ;
336
- signature = rsa . SignData ( TestData . HelloBytes , " SHA1" ) ;
335
+ signature = rsa . SignData ( TestData . HelloBytes , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
337
336
}
338
337
339
- using ( var rsa = new RSACryptoServiceProvider ( ) )
338
+ using ( RSA rsa = RSAFactory . Create ( ) )
340
339
{
341
340
rsa . ImportParameters ( TestData . RSA1024Params ) ;
342
- bool signatureMatched = rsa . VerifyData ( TestData . HelloBytes , " SHA1" , signature ) ;
341
+ bool signatureMatched = rsa . VerifyData ( TestData . HelloBytes , signature , HashAlgorithmName . SHA1 , RSASignaturePadding . Pkcs1 ) ;
343
342
344
343
Assert . False ( signatureMatched ) ;
345
344
}
@@ -619,10 +618,10 @@ private static void ExpectSignature(
619
618
// the signature is deterministic, so we can safely verify it here.
620
619
byte [ ] signature ;
621
620
622
- using ( var rsa = new RSACryptoServiceProvider ( ) )
621
+ using ( RSA rsa = RSAFactory . Create ( ) )
623
622
{
624
623
rsa . ImportParameters ( rsaParameters ) ;
625
- signature = rsa . SignData ( data , hashAlgorithmName ) ;
624
+ signature = rsa . SignData ( data , new HashAlgorithmName ( hashAlgorithmName ) , RSASignaturePadding . Pkcs1 ) ;
626
625
}
627
626
628
627
Assert . Equal ( expectedSignature , signature ) ;
@@ -640,10 +639,10 @@ private static void ExpectHashSignature(
640
639
// the signature is deterministic, so we can safely verify it here.
641
640
byte [ ] signature ;
642
641
643
- using ( var rsa = new RSACryptoServiceProvider ( ) )
642
+ using ( RSA rsa = RSAFactory . Create ( ) )
644
643
{
645
644
rsa . ImportParameters ( rsaParameters ) ;
646
- signature = rsa . SignHash ( dataHash , hashAlgorithmName ) ;
645
+ signature = rsa . SignHash ( dataHash , new HashAlgorithmName ( hashAlgorithmName ) , RSASignaturePadding . Pkcs1 ) ;
647
646
}
648
647
649
648
Assert . Equal ( expectedSignature , signature ) ;
@@ -663,10 +662,10 @@ private static void VerifySignature(
663
662
664
663
bool signatureMatched ;
665
664
666
- using ( var rsa = new RSACryptoServiceProvider ( ) )
665
+ using ( RSA rsa = RSAFactory . Create ( ) )
667
666
{
668
667
rsa . ImportParameters ( publicOnly ) ;
669
- signatureMatched = rsa . VerifyData ( data , hashAlgorithmName , signature ) ;
668
+ signatureMatched = rsa . VerifyData ( data , signature , new HashAlgorithmName ( hashAlgorithmName ) , RSASignaturePadding . Pkcs1 ) ;
670
669
}
671
670
672
671
Assert . True ( signatureMatched ) ;
@@ -686,22 +685,22 @@ private static void VerifyHashSignature(
686
685
687
686
bool signatureMatched ;
688
687
689
- using ( var rsa = new RSACryptoServiceProvider ( ) )
688
+ using ( RSA rsa = RSAFactory . Create ( ) )
690
689
{
691
690
rsa . ImportParameters ( publicOnly ) ;
692
- signatureMatched = rsa . VerifyHash ( dataHash , hashAlgorithmName , signature ) ;
691
+ signatureMatched = rsa . VerifyHash ( dataHash , signature , new HashAlgorithmName ( hashAlgorithmName ) , RSASignaturePadding . Pkcs1 ) ;
693
692
}
694
693
695
694
Assert . True ( signatureMatched ) ;
696
695
}
697
696
698
697
private static void SignAndVerify ( byte [ ] data , string hashAlgorithmName , RSAParameters rsaParameters )
699
698
{
700
- using ( var rsa = new RSACryptoServiceProvider ( ) )
699
+ using ( RSA rsa = RSAFactory . Create ( ) )
701
700
{
702
701
rsa . ImportParameters ( rsaParameters ) ;
703
- byte [ ] signature = rsa . SignData ( data , hashAlgorithmName ) ;
704
- bool signatureMatched = rsa . VerifyData ( data , hashAlgorithmName , signature ) ;
702
+ byte [ ] signature = rsa . SignData ( data , new HashAlgorithmName ( hashAlgorithmName ) , RSASignaturePadding . Pkcs1 ) ;
703
+ bool signatureMatched = rsa . VerifyData ( data , signature , new HashAlgorithmName ( hashAlgorithmName ) , RSASignaturePadding . Pkcs1 ) ;
705
704
Assert . True ( signatureMatched ) ;
706
705
}
707
706
}
0 commit comments