ipotex
05/13/2025, 5:47 AMimport crypto.hmac
class HKDF:
static hkdf_extract_ salt/ByteArray ikm/ByteArray -> ByteArray:
return hmac.hmac-sha256 --key=salt ikm
static hkdf_expand_ prk/ByteArray info/ByteArray length/int -> ByteArray:
hash_len := 32
n := (length + hash_len - 1) / hash_len
okm := #[]
previous := #[]
for i := 1; i <= n; i++:
data := previous + info + #[i]
previous = hmac.hmac-sha256 --key=prk data
okm += previous
return okm[0..length]
static derive --salt/ByteArray --ikm/ByteArray --info/ByteArray --length/int -> ByteArray:
prk := hkdf_extract_ salt ikm
return hkdf_expand_ prk info lengthfloitsch
05/13/2025, 6:12 AMHkdf.floitsch
05/13/2025, 6:14 AMdata and okm are using byte-array + operations. It could be interesting to use an io.Buffer instead (or if the size is know in the beginning just a ByteBuffer with replace calls.)
But that's just optimizations.floitsch
05/13/2025, 6:15 AMfloitsch
05/13/2025, 6:15 AMipotex
05/13/2025, 3:22 PM