-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdf.py
41 lines (33 loc) · 1.22 KB
/
kdf.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
from Crypto.Random import get_random_bytes
import hashlib
import struct
trans_5C = bytes((x ^ 0x5C) for x in range(256))
trans_36 = bytes((x ^ 0x36) for x in range(256))
def HmacSha256(key, data):
outer = hashlib.sha256()
inner = hashlib.sha256()
key = key.ljust(inner.block_size, b'\0')
outer.update(key.translate(trans_5C))
inner.update(key.translate(trans_36))
inner.update(data)
outer.update(inner.digest())
return outer.digest()
def HkdfExtract(XTS, SKM):
return HmacSha256(XTS, SKM)
def HkdfExpand(PRK, lastKey, CTX, i):
if lastKey is None:
lastKey = b''
return HmacSha256(PRK, lastKey + CTX + i)
def pbkdf2_function(key, XTS, count, i):
r = u = HmacSha256(key, XTS + struct.pack(">i", i))
for i in range(2, count + 1):
u = HmacSha256(key, u)
r = bytes(i ^ j for i, j in zip(r, u))
return r
def pbkdf2(key, XTS, count = 10000, dk_length = 64):
dk, h_length = b'', hashlib.sha256().digest_size
blocks = (dk_length // h_length) + (1 if dk_length % h_length else 0)
for i in range(1, blocks + 1):
dk += pbkdf2_function(key, XTS, count, i)
return dk[:dk_length]