@@ -22,20 +22,31 @@ public static byte[] CreateAuthenticationResponse(ReadOnlySpan<byte> challenge,
22
22
#endif
23
23
public static byte [ ] HashPassword ( ReadOnlySpan < byte > challenge , string password )
24
24
{
25
+ #if ! NET5_0_OR_GREATER
25
26
using var sha1 = SHA1 . Create ( ) ;
27
+ #endif
26
28
Span < byte > combined = stackalloc byte [ 40 ] ;
27
29
challenge . CopyTo ( combined ) ;
28
30
29
31
var passwordByteCount = Encoding . UTF8 . GetByteCount ( password ) ;
30
32
Span < byte > passwordBytes = stackalloc byte [ passwordByteCount ] ;
31
33
Encoding . UTF8 . GetBytes ( password . AsSpan ( ) , passwordBytes ) ;
32
34
Span < byte > hashedPassword = stackalloc byte [ 20 ] ;
35
+ #if NET5_0_OR_GREATER
36
+ SHA1 . TryHashData ( passwordBytes , hashedPassword , out _ ) ;
37
+ SHA1 . TryHashData ( hashedPassword , combined . Slice ( 20 ) , out _ ) ;
38
+ #else
33
39
sha1 . TryComputeHash ( passwordBytes , hashedPassword , out _ ) ;
34
40
sha1 . TryComputeHash ( hashedPassword , combined . Slice ( 20 ) , out _ ) ;
41
+ #endif
35
42
36
43
Span < byte > xorBytes = stackalloc byte [ 20 ] ;
44
+ #if NET5_0_OR_GREATER
45
+ SHA1 . TryHashData ( combined , xorBytes , out _ ) ;
46
+ #else
37
47
sha1 . TryComputeHash ( combined , xorBytes , out _ ) ;
38
- for ( int i = 0 ; i < hashedPassword . Length ; i ++ )
48
+ #endif
49
+ for ( var i = 0 ; i < hashedPassword . Length ; i ++ )
39
50
hashedPassword [ i ] ^= xorBytes [ i ] ;
40
51
41
52
return hashedPassword . ToArray ( ) ;
@@ -55,20 +66,34 @@ public static byte[] CreateScrambleResponse(ReadOnlySpan<byte> nonce, string pas
55
66
#endif
56
67
private static byte [ ] HashPasswordWithNonce ( ReadOnlySpan < byte > nonce , string password )
57
68
{
69
+ #if ! NET5_0_OR_GREATER
58
70
using var sha256 = SHA256 . Create ( ) ;
71
+ #endif
59
72
var passwordByteCount = Encoding . UTF8 . GetByteCount ( password ) ;
60
73
Span < byte > passwordBytes = stackalloc byte [ passwordByteCount ] ;
61
74
Encoding . UTF8 . GetBytes ( password . AsSpan ( ) , passwordBytes ) ;
62
75
63
76
Span < byte > hashedPassword = stackalloc byte [ 32 ] ;
77
+ #if NET5_0_OR_GREATER
78
+ SHA256 . TryHashData ( passwordBytes , hashedPassword , out _ ) ;
79
+ #else
64
80
sha256 . TryComputeHash ( passwordBytes , hashedPassword , out _ ) ;
81
+ #endif
65
82
66
83
Span < byte > combined = stackalloc byte [ 32 + nonce . Length ] ;
84
+ #if NET5_0_OR_GREATER
85
+ SHA256 . TryHashData ( hashedPassword , combined , out _ ) ;
86
+ #else
67
87
sha256 . TryComputeHash ( hashedPassword , combined , out _ ) ;
88
+ #endif
68
89
nonce . CopyTo ( combined . Slice ( 32 ) ) ;
69
90
70
91
Span < byte > xorBytes = stackalloc byte [ 32 ] ;
92
+ #if NET5_0_OR_GREATER
93
+ SHA256 . TryHashData ( combined , xorBytes , out _ ) ;
94
+ #else
71
95
sha256 . TryComputeHash ( combined , xorBytes , out _ ) ;
96
+ #endif
72
97
for ( int i = 0 ; i < hashedPassword . Length ; i ++ )
73
98
hashedPassword [ i ] ^= xorBytes [ i ] ;
74
99
0 commit comments