def hkdf_expand(pseudo_random_key, info=b"<STR_LIT>", length=<NUM_LIT:32>, hash=hashlib.sha512):
hash_len = hash().digest_size<EOL>length = int(length)<EOL>if length > <NUM_LIT:255> * hash_len:<EOL><INDENT>raise Exception("<STR_LIT>" %(hash_len, <NUM_LIT:255> * hash_len))<EOL><DEDENT>blocks_needed = length // hash_len + (<NUM_LIT:0> if length % hash_len == <NUM_LIT:0> else <NUM_LIT:1>) <EOL>okm = b"<STR_LIT>"<EOL>output_block = b"<STR_LIT>"<EOL>for counter in range(blocks_needed):<EOL><INDENT>output_block = hmac.new(pseudo_random_key, buffer(output_block + info + bytearray((counter + <NUM_LIT:1>,))),hash).digest()<EOL>okm += output_block<EOL><DEDENT>return okm[:length]<EOL>
Expand `pseudo_random_key` and `info` into a key of length `bytes` using
HKDF's expand function based on HMAC with the provided hash (default
SHA-512). See the HKDF draft RFC and paper for usage notes.