public class Hkdf extends Object
// Instantiate an Hkdf object with a hash function.
Hkdf hkdf = new Hkdf(Hash.SHA256);
// Using some protected input keying material (IKM), extract a pseudo-random key (PRK) with a
// random salt. Remember to store the salt so the key can be derived again.
SecretKey prk = hkdf.extract(Hkdf.randomSalt(), ikm);
// Expand the prk with some information related to your data and the length of the output key.
SecretKey derivedKey = hkdf.expand(prk, "id: 5".getBytes(StandardCharsets.UTF_8), 32);
HKDF is a generic means for generating derived keys. In some cases, you may want to use it in a different manner. Consult the RFC for security considerations, when to omit a salt, skipping the extraction step, etc.
| Modifier and Type | Method and Description |
|---|---|
byte[] |
expand(SecretKey key,
byte[] info,
int outputLength)
HKDF-Expand(PRK, info, L) -> OKM
|
SecretKey |
extract(SecretKey salt,
byte[] ikm)
HKDF-Extract(salt, IKM) -> PRK
|
SecretKey |
randomSalt()
Generates a random salt value to be used with
extract(javax.crypto.SecretKey, byte[]). |
static Hkdf |
usingDefaults() |
static Hkdf |
usingHash(Hash hash) |
static Hkdf |
usingProvider(Provider provider) |
public static Hkdf usingDefaults()
public static Hkdf usingHash(Hash hash)
hash - Supported hash function constantpublic static Hkdf usingProvider(Provider provider)
provider - provider for key derivation, particularly useful when using HSMspublic SecretKey extract(SecretKey salt, byte[] ikm)
salt - optional salt value (a non-secret random value); if not provided, it is set to a string of HashLen zeros.ikm - input keying materialpublic byte[] expand(SecretKey key, byte[] info, int outputLength)
key - a pseudorandom key of at least HashLen bytes (usually, the output from the extract step)info - context and application specific information (can be empty)outputLength - length of output keying material in bytes (<= 255*HashLen)public SecretKey randomSalt()
extract(javax.crypto.SecretKey, byte[]).Copyright © 2014–2016 Nikolche Mihajlovski and contributors. All rights reserved.