kumquat-buildroot/support/testing/tests/package/sample_python_hkdf.py
Julien Olivain 433ce2966f package/python-hkdf: new package
HMAC-based Extract-and-Expand Key Derivation Function (HKDF).

https://github.com/casebeer/python-hkdf

Signed-off-by: Julien Olivain <ju.o@free.fr>
[Thomas: justify the license.]
Signed-off-by: Thomas Petazzoni <thomas.petazzoni@bootlin.com>
2023-02-07 12:50:27 +01:00

23 lines
622 B
Python

import hashlib
from binascii import hexlify, unhexlify
from hkdf import Hkdf, hkdf_expand, hkdf_extract
salt = b"ThisIsTheSalt."
key_in = b"ThisIsTheSecretKey"
key_info = b"KeyInfo1"
key_len = 16
expected_key = unhexlify(b"b49d6cc9065b72f3a0859377d8bb7299")
prk = hkdf_extract(salt, input_key_material=key_in, hash=hashlib.sha512)
key1 = hkdf_expand(prk, info=key_info, length=key_len)
print("key1:", hexlify(key1))
assert key1 == expected_key
kdf = Hkdf(salt, input_key_material=key_in, hash=hashlib.sha512)
key2 = kdf.expand(info=key_info, length=key_len)
print("key2:", hexlify(key2))
assert key2 == expected_key