Rfc2898DeriveBytes.Pbkdf2 Method

Definition

Overloads

Pbkdf2(Byte[], Byte[], Int32, HashAlgorithmName, Int32)

Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs

Creates a PBKDF2 derived key from password bytes.

C#
public static byte[] Pbkdf2(byte[] password, byte[] salt, int iterations, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, int outputLength);

Parameters

password
Byte[]

The password used to derive the key.

salt
Byte[]

The key salt used to derive the key.

iterations
Int32

The number of iterations for the operation.

hashAlgorithm
HashAlgorithmName

The hash algorithm to use to derive the key.

outputLength
Int32

The size of key to derive.

Returns

Byte[]

A byte array containing the created PBKDF2 derived key.

Exceptions

password or salt is null.

outputLength is not zero or a positive value.

-or-

iterations is not a positive value.

hashAlgorithm has a Name that is empty or null.

hashAlgorithm is an unsupported hash algorithm. Supported algorithms are SHA1, SHA256, SHA384, and SHA512.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Pbkdf2(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Int32, HashAlgorithmName, Int32)

Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs

Creates a PBKDF2 derived key from password bytes.

C#
public static byte[] Pbkdf2(ReadOnlySpan<byte> password, ReadOnlySpan<byte> salt, int iterations, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, int outputLength);

Parameters

password
ReadOnlySpan<Byte>

The password used to derive the key.

salt
ReadOnlySpan<Byte>

The key salt used to derive the key.

iterations
Int32

The number of iterations for the operation.

hashAlgorithm
HashAlgorithmName

The hash algorithm to use to derive the key.

outputLength
Int32

The size of key to derive.

Returns

Byte[]

A byte array containing the created PBKDF2 derived key.

Exceptions

outputLength is not zero or a positive value.

-or-

iterations is not a positive value.

hashAlgorithm has a Name that is empty or null.

hashAlgorithm is an unsupported hash algorithm. Supported algorithms are SHA1, SHA256, SHA384, and SHA512.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Pbkdf2(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, HashAlgorithmName)

Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs

Fills a buffer with a PBKDF2 derived key.

C#
public static void Pbkdf2(ReadOnlySpan<byte> password, ReadOnlySpan<byte> salt, Span<byte> destination, int iterations, System.Security.Cryptography.HashAlgorithmName hashAlgorithm);

Parameters

password
ReadOnlySpan<Byte>

The password used to derive the key.

salt
ReadOnlySpan<Byte>

The key salt used to derive the key.

destination
Span<Byte>

The buffer to fill with a derived key.

iterations
Int32

The number of iterations for the operation.

hashAlgorithm
HashAlgorithmName

The hash algorithm to use to derive the key.

Exceptions

iterations is not a positive value.

hashAlgorithm has a Name that is empty or null.

hashAlgorithm is an unsupported hash algorithm. Supported algorithms are SHA1, SHA256, SHA384, and SHA512.

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Pbkdf2(ReadOnlySpan<Char>, ReadOnlySpan<Byte>, Int32, HashAlgorithmName, Int32)

Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs

Creates a PBKDF2 derived key from a password.

C#
public static byte[] Pbkdf2(ReadOnlySpan<char> password, ReadOnlySpan<byte> salt, int iterations, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, int outputLength);

Parameters

password
ReadOnlySpan<Char>

The password used to derive the key.

salt
ReadOnlySpan<Byte>

The key salt used to derive the key.

iterations
Int32

The number of iterations for the operation.

hashAlgorithm
HashAlgorithmName

The hash algorithm to use to derive the key.

outputLength
Int32

The size of key to derive.

Returns

Byte[]

A byte array of length outputLength that is filled with pseudo-random key bytes.

Exceptions

outputLength is not zero or a positive value.

-or-

iterations is not a positive value.

hashAlgorithm has a Name that is empty or null.

hashAlgorithm is an unsupported hash algorithm. Supported algorithms are SHA1, SHA256, SHA384, and SHA512.

password contains text that cannot be converted to UTF8.

Remarks

The password will be converted to bytes using the UTF8 encoding. For other encodings, convert the password string to bytes using the appropriate Encoding and use Pbkdf2(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Int32, HashAlgorithmName, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Pbkdf2(ReadOnlySpan<Char>, ReadOnlySpan<Byte>, Span<Byte>, Int32, HashAlgorithmName)

Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs

Fills a buffer with a PBKDF2 derived key.

C#
public static void Pbkdf2(ReadOnlySpan<char> password, ReadOnlySpan<byte> salt, Span<byte> destination, int iterations, System.Security.Cryptography.HashAlgorithmName hashAlgorithm);

Parameters

password
ReadOnlySpan<Char>

The password used to derive the key.

salt
ReadOnlySpan<Byte>

The key salt used to derive the key.

destination
Span<Byte>

The buffer to fill with a derived key.

iterations
Int32

The number of iterations for the operation.

hashAlgorithm
HashAlgorithmName

The hash algorithm to use to derive the key.

Exceptions

iterations is not a positive value.

hashAlgorithm has a Name that is empty or null.

hashAlgorithm is an unsupported hash algorithm. Supported algorithms are SHA1, SHA256, SHA384, and SHA512.

password contains text that cannot be converted to UTF8.

Remarks

The password will be converted to bytes using the UTF8 encoding. For other encodings, convert the password string to bytes using the appropriate Encoding and use Pbkdf2(ReadOnlySpan<Byte>, ReadOnlySpan<Byte>, Span<Byte>, Int32, HashAlgorithmName).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9

Pbkdf2(String, Byte[], Int32, HashAlgorithmName, Int32)

Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs
Source:
Rfc2898DeriveBytes.OneShot.cs

Creates a PBKDF2 derived key from a password.

C#
public static byte[] Pbkdf2(string password, byte[] salt, int iterations, System.Security.Cryptography.HashAlgorithmName hashAlgorithm, int outputLength);

Parameters

password
String

The password used to derive the key.

salt
Byte[]

The key salt used to derive the key.

iterations
Int32

The number of iterations for the operation.

hashAlgorithm
HashAlgorithmName

The hash algorithm to use to derive the key.

outputLength
Int32

The size of key to derive.

Returns

Byte[]

A byte array of length outputLength that is filled with pseudo-random key bytes.

Exceptions

password or salt is null.

outputLength is not zero or a positive value.

-or-

iterations is not a positive value.

hashAlgorithm has a Name that is empty or null.

hashAlgorithm is an unsupported hash algorithm. Supported algorithms are SHA1, SHA256, SHA384, and SHA512.

password contains text that cannot be converted to UTF8.

Remarks

The password will be converted to bytes using the UTF8 encoding. For other encodings, convert the password string to bytes using the appropriate Encoding and use Pbkdf2(Byte[], Byte[], Int32, HashAlgorithmName, Int32).

Applies to

.NET 9 and other versions
Product Versions
.NET 6, 7, 8, 9