@@ -32,11 +32,16 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
32
32
{
33
33
// First 32 bytes are server scramble
34
34
var serverScramble = authenticationData . Slice ( 0 , 32 ) ;
35
-
35
+
36
36
// 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
37
41
var clientScramble = new byte [ 32 ] ;
38
42
using var randomNumberGenerator = RandomNumberGenerator . Create ( ) ;
39
43
randomNumberGenerator . GetBytes ( clientScramble ) ;
44
+ #endif
40
45
41
46
// Parse extended salt from remaining auth data
42
47
var extendedSalt = authenticationData . Slice ( 32 ) ;
@@ -49,7 +54,10 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
49
54
var salt = extendedSalt . Slice ( 2 ) ;
50
55
51
56
// 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
53
61
using ( var pbkdf2 = new Rfc2898DeriveBytes (
54
62
Encoding . UTF8 . GetBytes ( password ) ,
55
63
salt . ToArray ( ) ,
@@ -58,6 +66,7 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
58
66
{
59
67
privateKey = pbkdf2 . GetBytes ( 32 ) ;
60
68
}
69
+ #endif
61
70
62
71
// Generate Ed25519 keypair and sign concatenated scrambles
63
72
// var keyPair = Chaos.NaCl.Ed25519.GenerateKeyPair(privateKey);
@@ -71,7 +80,7 @@ public byte[] CreateResponse(string password, ReadOnlySpan<byte> authenticationD
71
80
72
81
// Return client scramble followed by signature
73
82
var response = new byte [ clientScramble . Length + signature . Length ] ;
74
- clientScramble . CopyTo ( response ) ;
83
+ clientScramble . CopyTo ( response . AsSpan ( ) ) ;
75
84
signature . CopyTo ( response . AsSpan ( clientScramble . Length ) ) ;
76
85
77
86
return response ;
0 commit comments