Skip to content

Commit d1eec8d

Browse files
committed
Improve code for .NET 6.
1 parent ad5ed09 commit d1eec8d

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/MySqlConnector.Authentication.Ed25519/ParsecAuthenticationPlugin.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,16 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
3232
{
3333
// First 32 bytes are server scramble
3434
var serverScramble = authenticationData.Slice(0, 32);
35-
35+
3636
// Generate client scramble
37+
#if NET6_0_OR_GREATER || NETSTANDARD2_1_OR_GREATER
38+
Span<byte> clientScramble = stackalloc byte[32];
39+
RandomNumberGenerator.Fill(clientScramble);
40+
#else
3741
var clientScramble = new byte[32];
3842
using var randomNumberGenerator = RandomNumberGenerator.Create();
3943
randomNumberGenerator.GetBytes(clientScramble);
44+
#endif
4045

4146
// Parse extended salt from remaining auth data
4247
var extendedSalt = authenticationData.Slice(32);
@@ -49,7 +54,10 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
4954
var salt = extendedSalt.Slice(2);
5055

5156
// Derive private key using PBKDF2-SHA512
52-
var privateKey = new byte[32];
57+
byte[] privateKey;
58+
#if NET6_0_OR_GREATER
59+
privateKey = Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterationCount, HashAlgorithmName.SHA512, 32);
60+
#else
5361
using (var pbkdf2 = new Rfc2898DeriveBytes(
5462
Encoding.UTF8.GetBytes(password),
5563
salt.ToArray(),
@@ -58,6 +66,7 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
5866
{
5967
privateKey = pbkdf2.GetBytes(32);
6068
}
69+
#endif
6170

6271
// Generate Ed25519 keypair and sign concatenated scrambles
6372
// var keyPair = Chaos.NaCl.Ed25519.GenerateKeyPair(privateKey);
@@ -71,7 +80,7 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
7180

7281
// Return client scramble followed by signature
7382
var response = new byte[clientScramble.Length + signature.Length];
74-
clientScramble.CopyTo(response);
83+
clientScramble.CopyTo(response.AsSpan());
7584
signature.CopyTo(response.AsSpan(clientScramble.Length));
7685

7786
return response;

0 commit comments

Comments
 (0)