Skip to content

Commit d9579be

Browse files
committed
Use cryptographic one-shot operations.
1 parent dbd40e5 commit d9579be

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/MySqlConnector/Protocol/Serialization/AuthenticationUtility.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,31 @@ public static byte[] CreateAuthenticationResponse(ReadOnlySpan<byte> challenge,
2222
#endif
2323
public static byte[] HashPassword(ReadOnlySpan<byte> challenge, string password)
2424
{
25+
#if !NET5_0_OR_GREATER
2526
using var sha1 = SHA1.Create();
27+
#endif
2628
Span<byte> combined = stackalloc byte[40];
2729
challenge.CopyTo(combined);
2830

2931
var passwordByteCount = Encoding.UTF8.GetByteCount(password);
3032
Span<byte> passwordBytes = stackalloc byte[passwordByteCount];
3133
Encoding.UTF8.GetBytes(password.AsSpan(), passwordBytes);
3234
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
3339
sha1.TryComputeHash(passwordBytes, hashedPassword, out _);
3440
sha1.TryComputeHash(hashedPassword, combined.Slice(20), out _);
41+
#endif
3542

3643
Span<byte> xorBytes = stackalloc byte[20];
44+
#if NET5_0_OR_GREATER
45+
SHA1.TryHashData(combined, xorBytes, out _);
46+
#else
3747
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++)
3950
hashedPassword[i] ^= xorBytes[i];
4051

4152
return hashedPassword.ToArray();
@@ -55,20 +66,34 @@ public static byte[] CreateScrambleResponse(ReadOnlySpan<byte> nonce, string pas
5566
#endif
5667
private static byte[] HashPasswordWithNonce(ReadOnlySpan<byte> nonce, string password)
5768
{
69+
#if !NET5_0_OR_GREATER
5870
using var sha256 = SHA256.Create();
71+
#endif
5972
var passwordByteCount = Encoding.UTF8.GetByteCount(password);
6073
Span<byte> passwordBytes = stackalloc byte[passwordByteCount];
6174
Encoding.UTF8.GetBytes(password.AsSpan(), passwordBytes);
6275

6376
Span<byte> hashedPassword = stackalloc byte[32];
77+
#if NET5_0_OR_GREATER
78+
SHA256.TryHashData(passwordBytes, hashedPassword, out _);
79+
#else
6480
sha256.TryComputeHash(passwordBytes, hashedPassword, out _);
81+
#endif
6582

6683
Span<byte> combined = stackalloc byte[32 + nonce.Length];
84+
#if NET5_0_OR_GREATER
85+
SHA256.TryHashData(hashedPassword, combined, out _);
86+
#else
6787
sha256.TryComputeHash(hashedPassword, combined, out _);
88+
#endif
6889
nonce.CopyTo(combined.Slice(32));
6990

7091
Span<byte> xorBytes = stackalloc byte[32];
92+
#if NET5_0_OR_GREATER
93+
SHA256.TryHashData(combined, xorBytes, out _);
94+
#else
7195
sha256.TryComputeHash(combined, xorBytes, out _);
96+
#endif
7297
for (int i = 0; i < hashedPassword.Length; i++)
7398
hashedPassword[i] ^= xorBytes[i];
7499

0 commit comments

Comments
 (0)