Skip to content

Commit

Permalink
Clean up NetCore PBKDF2 provider. (#31391)
Browse files Browse the repository at this point in the history
  • Loading branch information
vcsjones authored Mar 30, 2021
1 parent e96330f commit d0f23a2
Showing 1 changed file with 1 addition and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,13 @@ namespace Microsoft.AspNetCore.Cryptography.KeyDerivation.PBKDF2
/// </summary>
internal sealed class NetCorePbkdf2Provider : IPbkdf2Provider
{
private static readonly ManagedPbkdf2Provider _fallbackProvider = new ManagedPbkdf2Provider();

public byte[] DeriveKey(string password, byte[] salt, KeyDerivationPrf prf, int iterationCount, int numBytesRequested)
{
Debug.Assert(password != null);
Debug.Assert(salt != null);
Debug.Assert(iterationCount > 0);
Debug.Assert(numBytesRequested > 0);

if (salt.Length < 8)
{
// Rfc2898DeriveBytes enforces the 8 byte recommendation.
// To maintain compatibility, we call into ManagedPbkdf2Provider for salts shorter than 8 bytes
// because we can't use Rfc2898DeriveBytes with this salt.
return _fallbackProvider.DeriveKey(password, salt, prf, iterationCount, numBytesRequested);
}
else
{
return DeriveKeyImpl(password, salt, prf, iterationCount, numBytesRequested);
}
}

private static byte[] DeriveKeyImpl(string password, byte[] salt, KeyDerivationPrf prf, int iterationCount, int numBytesRequested)
{
HashAlgorithmName algorithmName;
switch (prf)
{
Expand All @@ -54,7 +37,7 @@ private static byte[] DeriveKeyImpl(string password, byte[] salt, KeyDerivationP
throw new ArgumentOutOfRangeException();
}

return Rfc2898DeriveBytes.Pbkdf2(Encoding.UTF8.GetBytes(password), salt, iterationCount, algorithmName, numBytesRequested);
return Rfc2898DeriveBytes.Pbkdf2(password, salt, iterationCount, algorithmName, numBytesRequested);
}
}
}
Expand Down

0 comments on commit d0f23a2

Please sign in to comment.