pdkdf2.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:lampost_lib 作者: genzgd 项目源码 文件源码
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])
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号