public final class KDF extends Object
KDF objects are instantiated with the getInstance family of methods.
The class has two derive methods, deriveKey and deriveData. The deriveKey method accepts an algorithm name and returns a SecretKey object with the specified algorithm. The deriveData method returns a byte array of raw data.
API Usage Example:
KDF kdfHkdf = KDF.getInstance("HKDF-SHA256");
AlgorithmParameterSpec derivationSpec =
HKDFParameterSpec.ofExtract()
.addIKM(ikm)
.addSalt(salt).thenExpand(info, 32);
SecretKey sKey = kdfHkdf.deriveKey("AES", derivationSpec);
getInstance methods, the implementation delays the selection of the provider until the deriveKey or deriveData method is called. This is called delayed provider selection. The primary reason this is done is to ensure that the selected provider can handle the key material that is passed to those methods - for example, the key material may reside on a hardware device that only a specific KDF provider can utilize. The
getInstance method returns a KDF object as long as there exists at least one registered security provider that implements the algorithm and supports the optional parameters. The delayed provider selection process traverses the list of registered security providers, starting with the most preferred Provider. The first provider that supports the specified algorithm, optional parameters, and key material is selected. If the getProviderName or getParameters method is called before the deriveKey or deriveData methods, the first provider supporting the KDF algorithm and optional KDFParameters is chosen. This provider may not support the key material that is subsequently passed to the deriveKey or deriveData methods. Therefore, it is recommended not to call the getProviderName or getParameters methods until after a key derivation operation. Once a provider is selected, it cannot be changed.
| Modifier and Type | Method | Description |
|---|---|---|
byte[] |
deriveData |
Derives a key, returns raw data as a byte array. |
SecretKey |
deriveKey |
Derives a key, returned as a SecretKey object. |
String |
getAlgorithm() |
Returns the algorithm name of this KDF object. |
static KDF |
getInstance |
Returns a KDF object that implements the specified algorithm. |
static KDF |
getInstance |
Returns a KDF object that implements the specified algorithm from the specified security provider. |
static KDF |
getInstance |
Returns a KDF object that implements the specified algorithm from the specified security provider. |
static KDF |
getInstance |
Returns a KDF object that implements the specified algorithm and is initialized with the specified parameters. |
static KDF |
getInstance |
Returns a KDF object that implements the specified algorithm from the specified provider and is initialized with the specified parameters. |
static KDF |
getInstance |
Returns a KDF object that implements the specified algorithm from the specified provider and is initialized with the specified parameters. |
KDFParameters |
getParameters() |
Returns the KDFParameters used with this KDF object. |
String |
getProviderName() |
Returns the name of the provider. |
public String getAlgorithm()
KDF object.KDF objectpublic String getProviderName()
public KDFParameters getParameters()
KDFParameters used with this KDF object. The returned parameters may be the same that were used to initialize this KDF object, or may contain additional default or random parameter values used by the underlying KDF algorithm. If the required parameters were not supplied and can be generated by the KDF object, the generated parameters are returned; otherwise null is returned.
KDF object, or null
public static KDF getInstance(String algorithm) throws NoSuchAlgorithmException
KDF object that implements the specified algorithm.jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different than the order of providers returned by Security.getProviders().algorithm - the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.KDF objectNoSuchAlgorithmException - if no Provider supports a KDF implementation for the specified algorithmNullPointerException - if algorithm is null
public static KDF getInstance(String algorithm, String provider) throws NoSuchAlgorithmException, NoSuchProviderException
KDF object that implements the specified algorithm from the specified security provider. The specified provider must be registered in the security provider list.algorithm - the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.provider - the provider to use for this key derivationKDF objectNoSuchAlgorithmException - if the specified provider does not support the specified KDF algorithmNoSuchProviderException - if the specified provider is not registered in the security provider listNullPointerException - if algorithm or provider is null
public static KDF getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
KDF object that implements the specified algorithm from the specified security provider.algorithm - the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.provider - the provider to use for this key derivationKDF objectNoSuchAlgorithmException - if the specified provider does not support the specified KDF algorithmNullPointerException - if algorithm or provider is null
public static KDF getInstance(String algorithm, KDFParameters kdfParameters) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException
KDF object that implements the specified algorithm and is initialized with the specified parameters.jdk.security.provider.preferred Security property to determine the preferred provider order for the specified algorithm. This may be different than the order of providers returned by Security.getProviders().algorithm - the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.kdfParameters - the KDFParameters used to configure the derivation algorithm or null if no parameters are providedKDF objectNoSuchAlgorithmException - if no Provider supports a KDF implementation for the specified algorithmInvalidAlgorithmParameterException - if at least one Provider supports a KDF implementation for the specified algorithm but none of them support the specified parametersNullPointerException - if algorithm is null
public static KDF getInstance(String algorithm, KDFParameters kdfParameters, String provider) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidAlgorithmParameterException
KDF object that implements the specified algorithm from the specified provider and is initialized with the specified parameters. The specified provider must be registered in the security provider list.algorithm - the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.kdfParameters - the KDFParameters used to configure the derivation algorithm or null if no parameters are providedprovider - the provider to use for this key derivationKDF objectNoSuchAlgorithmException - if the specified provider does not support the specified KDF algorithmNoSuchProviderException - if the specified provider is not registered in the security provider listInvalidAlgorithmParameterException - if the specified provider supports the specified KDF algorithm but does not support the specified parametersNullPointerException - if algorithm or provider is null
public static KDF getInstance(String algorithm, KDFParameters kdfParameters, Provider provider) throws NoSuchAlgorithmException, InvalidAlgorithmParameterException
KDF object that implements the specified algorithm from the specified provider and is initialized with the specified parameters.algorithm - the key derivation algorithm to use. See the KDF section in the Java Security Standard Algorithm Names Specification for information about standard KDF algorithm names.kdfParameters - the KDFParameters used to configure the derivation algorithm or null if no parameters are providedprovider - the provider to use for this key derivationKDF objectNoSuchAlgorithmException - if the specified provider does not support the specified KDF algorithmInvalidAlgorithmParameterException - if the specified provider supports the specified KDF algorithm but does not support the specified parametersNullPointerException - if algorithm or provider is null
public SecretKey deriveKey(String alg, AlgorithmParameterSpec derivationSpec) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException
SecretKey object.alg - the algorithm of the resultant SecretKey object. See the SecretKey Algorithms section in the Java Security Standard Algorithm Names Specification for information about standard secret key algorithm names.derivationSpec - the object describing the inputs to the derivation functionInvalidAlgorithmParameterException - if the information contained within the derivationSpec is invalid or if the combination of alg and the derivationSpec results in something invalidNoSuchAlgorithmException - if alg is empty or invalidNullPointerException - if alg or derivationSpec is nullpublic byte[] deriveData(AlgorithmParameterSpec derivationSpec) throws InvalidAlgorithmParameterException
derivationSpec - the object describing the inputs to the derivation functionInvalidAlgorithmParameterException - if the information contained within the derivationSpec is invalidUnsupportedOperationException - if the derived keying material is not extractableNullPointerException - if derivationSpec is null
© 1993, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/25/docs/api/java.base/javax/crypto/KDF.html