Skip to content

End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

OpenSSL::KDF.hkdf

KDF.hkdf(ikm, salt:, info:, length:, hash:) → String

HMAC-based Extract-and-Expand Key Derivation Function (HKDF) as specified in RFC 5869.

New in OpenSSL 1.1.0.

Parameters

ikm

The input keying material.

salt

The salt.

info

The context and application specific information.

length

The output length in octets. Must be <= 255 * HashLen, where HashLen is the length of the hash function output in octets.

hash

The hash function.

Example

Example
# The values from https://datatracker.ietf.org/doc/html/rfc5869#appendix-A.1
ikm = ["0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b"].pack("H*")
salt = ["000102030405060708090a0b0c"].pack("H*")
info = ["f0f1f2f3f4f5f6f7f8f9"].pack("H*")
p OpenSSL::KDF.hkdf(ikm, salt: salt, info: info, length: 42, hash: "SHA256").unpack1("H*")
# => "3cb25f25faacd57a90434f64d0362f2a2d2d0a90cf1a5a4c5db02d56ecc4c5bf34007208d5b887185865"
See also .pbkdf2_hmac .scrypt also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0