GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • java.evolved
  • ←
  • →

This view

  • Original page
  • New address

GeezerVison is showing javaevolved.dev — Key Derivation Functions | java.evolved

Home / Security / Key Derivation Functions
Security Advanced

Key Derivation Functions

  • 🐛 Report a code issue
  • 🌐 Report a translation issue
  • 💡 Suggest a new pattern

Derive cryptographic keys using the standard KDF API.

Security Cryptography
Code Comparison
✕ Java 8
SecretKeyFactory factory =
    SecretKeyFactory.getInstance(
        "PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(
    password, salt, 10000, 256);
SecretKey key =
    factory.generateSecret(spec);
✓ Java 25
var kdf = KDF.getInstance("HKDF-SHA256");
SecretKey key = kdf.deriveKey(
    "AES",
    KDF.HKDFParameterSpec
        .ofExtract()
        .addIKM(inputKey)
        .addSalt(salt)
        .thenExpand(info, 32)
        .build()
);
Share 𝕏
See a problem with this code? Let us know.
Why the modern way wins
📐

Clean API

Builder pattern instead of awkward KeySpec constructors.

🔧

HKDF support

Modern HKDF algorithm alongside PBKDF2.

🛡️

Standard

Unified API for all key derivation algorithms.

Old Approach
Manual PBKDF2
Modern Approach
KDF API
Since JDK
25
Difficulty
Advanced
JDK Support
Key Derivation Functions
Available

Finalized in JDK 25 LTS (JEP 510, Sept 2025).

How it works

The KDF API provides a standard interface for key derivation functions including HKDF. It replaces the awkward SecretKeyFactory + PBEKeySpec pattern with a clean builder API.

Related Documentation
Key Derivation Functions (JEP 478) ↗
Proof
View proof source ↗
Related patterns
Security Advanced

PEM encoding/decoding

Java 8
String pem = "-----BEGIN CERTIFICATE-----\n"
    + Base64.getMimeEncoder()
        .encodeToString(
            cert.getEncoded())
    + "\n-----END CERTIFICATE-----";
Java 25 (Preview)
// Encode to PEM
String pem = PEMEncoder.of()
    .encodeToString(cert);
// Decode from PEM
var cert = PEMDecoder.of()
    .decode(pemString);
Hover to see modern ➜
JDK 25+ →
Security Beginner

Strong random generation

Java 8
// Default algorithm — may not be
// the strongest available
SecureRandom random =
    new SecureRandom();
byte[] bytes = new byte[32];
random.nextBytes(bytes);
Java 9+
// Platform's strongest algorithm
SecureRandom random =
    SecureRandom.getInstanceStrong();
byte[] bytes = new byte[32];
random.nextBytes(bytes);
Hover to see modern ➜
JDK 9+ →
Security Intermediate

TLS 1.3 by default

Java 8
SSLContext ctx =
    SSLContext.getInstance("TLSv1.2");
ctx.init(null, trustManagers, null);
SSLSocketFactory factory =
    ctx.getSocketFactory();
// Must specify protocol version
Java 11+
// TLS 1.3 is the default!
var client = HttpClient.newBuilder()
    .sslContext(SSLContext.getDefault())
    .build();
// Already using TLS 1.3
Hover to see modern ➜
JDK 11+ →
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.