Heirarchial Key Derivation Function
Created on 2020-11-15T16:27:09-06:00
- Allows you to derive other possibly longer keys from another key.
- Easy for GPUs and ASICs to implement though.
- Its a standard but I prefer Blake2X over this.
Python code
#!/usr/bin/env python3
import hashlib
import hmac
from math import ceil
hash_len = 32
def hmac_sha256(key, data):
return hmac.new(key, data, hashlib.sha256).digest()
def hkdf(length: int, ikm, salt: bytes = b"", info: bytes = b"") -> bytes:
if len(salt) == 0:
salt = bytes([0] * hash_len)
prk = hmac_sha256(salt, ikm)
t = b""
okm = b""
for i in range(ceil(length / hash_len)):
t = hmac_sha256(prk, t + info + bytes([1 + i]))
okm += t
return okm[:length]