@@ -23,7 +23,9 @@ public static byte[] HashPassword(ReadOnlySpan<byte> challenge, string password)
23
23
Span < byte > combined = stackalloc byte [ 40 ] ;
24
24
challenge . CopyTo ( combined ) ;
25
25
26
- var passwordBytes = Encoding . UTF8 . GetBytes ( password ) ;
26
+ var passwordByteCount = Encoding . UTF8 . GetByteCount ( password ) ;
27
+ Span < byte > passwordBytes = stackalloc byte [ passwordByteCount ] ;
28
+ Encoding . UTF8 . GetBytes ( password . AsSpan ( ) , passwordBytes ) ;
27
29
Span < byte > hashedPassword = stackalloc byte [ 20 ] ;
28
30
sha1 . TryComputeHash ( passwordBytes , hashedPassword , out _ ) ;
29
31
sha1 . TryComputeHash ( hashedPassword , combined . Slice ( 20 ) , out _ ) ;
@@ -36,7 +38,7 @@ public static byte[] HashPassword(ReadOnlySpan<byte> challenge, string password)
36
38
return hashedPassword . ToArray ( ) ;
37
39
}
38
40
39
- public static byte [ ] CreateScrambleResponse ( byte [ ] nonce , string password )
41
+ public static byte [ ] CreateScrambleResponse ( ReadOnlySpan < byte > nonce , string password )
40
42
{
41
43
var scrambleResponse = string . IsNullOrEmpty ( password )
42
44
? Utility . EmptyByteArray
@@ -45,23 +47,26 @@ public static byte[] CreateScrambleResponse(byte[] nonce, string password)
45
47
return scrambleResponse ;
46
48
}
47
49
48
- private static byte [ ] HashPasswordWithNonce ( byte [ ] nonce , string password )
50
+ private static byte [ ] HashPasswordWithNonce ( ReadOnlySpan < byte > nonce , string password )
49
51
{
50
52
using var sha256 = SHA256 . Create ( ) ;
51
- var passwordBytes = Encoding . UTF8 . GetBytes ( password ) ;
52
- var hashedPassword = sha256 . ComputeHash ( passwordBytes ) ;
53
+ var passwordByteCount = Encoding . UTF8 . GetByteCount ( password ) ;
54
+ Span < byte > passwordBytes = stackalloc byte [ passwordByteCount ] ;
55
+ Encoding . UTF8 . GetBytes ( password . AsSpan ( ) , passwordBytes ) ;
53
56
54
- var doubleHashedPassword = sha256 . ComputeHash ( hashedPassword ) ;
55
- var combined = new byte [ doubleHashedPassword . Length + nonce . Length ] ;
57
+ Span < byte > hashedPassword = stackalloc byte [ 32 ] ;
58
+ sha256 . TryComputeHash ( passwordBytes , hashedPassword , out _ ) ;
56
59
57
- Buffer . BlockCopy ( doubleHashedPassword , 0 , combined , 0 , doubleHashedPassword . Length ) ;
58
- Buffer . BlockCopy ( nonce , 0 , combined , doubleHashedPassword . Length , nonce . Length ) ;
60
+ Span < byte > combined = stackalloc byte [ 32 + nonce . Length ] ;
61
+ sha256 . TryComputeHash ( hashedPassword , combined , out _ ) ;
62
+ nonce . CopyTo ( combined . Slice ( 32 ) ) ;
59
63
60
- var xorBytes = sha256 . ComputeHash ( combined ) ;
64
+ Span < byte > xorBytes = stackalloc byte [ 32 ] ;
65
+ sha256 . TryComputeHash ( combined , xorBytes , out _ ) ;
61
66
for ( int i = 0 ; i < hashedPassword . Length ; i ++ )
62
67
hashedPassword [ i ] ^= xorBytes [ i ] ;
63
68
64
- return hashedPassword ;
69
+ return hashedPassword . ToArray ( ) ;
65
70
}
66
71
}
67
72
}
0 commit comments