38
38
using System . Security . Authentication ;
39
39
using System . Security . Cryptography ;
40
40
using System . Security . Cryptography . X509Certificates ;
41
-
42
41
#if ! ( NET8_0_OR_GREATER )
43
42
using System . Runtime . Serialization ;
44
43
#endif
@@ -303,7 +302,8 @@ private static bool ValidateServerCertificate(
303
302
/// <returns>The secure hash as a hex string.</returns>
304
303
private static string _MD5Hash ( string str )
305
304
{
306
- return ComputeHash ( str , "MD5" ) ;
305
+ using ( var hasher = MD5 . Create ( ) )
306
+ return ComputeHash ( hasher , str ) ;
307
307
}
308
308
309
309
/// <summary>
@@ -313,32 +313,24 @@ private static string _MD5Hash(string str)
313
313
/// <returns>The secure hash as a hex string.</returns>
314
314
private static string Sha256Hash ( string str )
315
315
{
316
- return ComputeHash ( str , "SHA256" ) ;
316
+ using ( var hasher = SHA256 . Create ( ) )
317
+ return ComputeHash ( hasher , str ) ;
317
318
}
318
319
319
- private static string ComputeHash ( string input , string method )
320
+ private static string ComputeHash ( HashAlgorithm hasher , string input )
320
321
{
321
- if ( input == null )
322
+ if ( hasher == null || input == null )
322
323
return null ;
323
324
324
325
var enc = new UTF8Encoding ( ) ;
325
326
byte [ ] bytes = enc . GetBytes ( input ) ;
326
-
327
- using ( var hasher = HashAlgorithm . Create ( method ) )
328
- {
329
- if ( hasher != null )
330
- {
331
- byte [ ] hash = hasher . ComputeHash ( bytes ) ;
332
- return BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLowerInvariant ( ) ;
333
- }
334
- }
335
-
336
- return null ;
327
+ byte [ ] hash = hasher . ComputeHash ( bytes ) ;
328
+ return BitConverter . ToString ( hash ) . Replace ( "-" , "" ) . ToLowerInvariant ( ) ;
337
329
}
338
330
339
331
private static string GenerateNonce ( )
340
332
{
341
- using ( var rngCsProvider = new RNGCryptoServiceProvider ( ) )
333
+ using ( var rngCsProvider = RandomNumberGenerator . Create ( ) )
342
334
{
343
335
var nonceBytes = new byte [ NONCE_LENGTH ] ;
344
336
rngCsProvider . GetBytes ( nonceBytes ) ;
@@ -492,7 +484,7 @@ public static Stream ConnectStream(Uri uri, IWebProxy proxy, bool nodelay, int t
492
484
if ( UseSSL ( uri ) )
493
485
{
494
486
SslStream sslStream = new SslStream ( stream , false , ValidateServerCertificate , null ) ;
495
- sslStream . AuthenticateAsClient ( "" , null , SslProtocols . Tls | SslProtocols . Tls11 | SslProtocols . Tls12 , true ) ;
487
+ sslStream . AuthenticateAsClient ( "" , null , SslProtocols . Tls12 , true ) ;
496
488
497
489
stream = sslStream ;
498
490
}
0 commit comments