def pbkdf2_bin(data, salt, iterations=1000, key_len=24, hash_func=None):
"""Returns a binary digest for the PBKDF2 hash algorithm of `data`
with the given `salt`. It iterates `iterations` time and produces a
key of `key_len` bytes. By default SHA-1 is used as hash function,
a different hashlib `hash_func` can be provided.
"""
hash_func = hash_func or hashlib.sha1
mac = hmac.new(data, None, hash_func)
def _pseudo_random(x):
h = mac.copy()
h.update(x)
return h.digest()
buf = bytearray()
for block in range(1, -(-key_len // mac.digest_size) + 1):
rv = u = _pseudo_random(salt + _pack_int(block))
for i in range(iterations - 1):
u = _pseudo_random(u)
rv = starmap(xor, zip(rv, u))
buf.extend(rv)
return bytes(buf[:key_len])
评论列表
文章目录