Skip to content

Clean up NetCore PBKDF2 provider #31391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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