-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhkdf.py
More file actions
28 lines (22 loc) · 787 Bytes
/
hkdf.py
File metadata and controls
28 lines (22 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/usr/bin/env python3
import hashlib, os, sys
sys.path.append("..")
from math import ceil
from Message_Authentication_Codes.hmac import hmac
def hmac_hash(key, message, hash_object):
return hmac(key, message, hash_object)
def hkdf(hash_object, output_size, input_key, salt=b"", data=b""):
"""Key derivation function"""
hash_len = hash_object().digest_size
temp = b""
output = b""
if len(salt) == 0:
salt = bytes([0] * hash_len)
key_output = hmac_hash(salt, input_key, hash_object)
for i in range(ceil(output_size / hash_len)):
temp = hmac_hash(key_output, temp + data + bytes([1 + i]), hash_object)
output += temp
return output[:output_size]
if __name__ == '__main__':
test = hkdf(hashlib.sha512, 24, os.urandom(16))
print(test.hex())