hashers.py 文件源码

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

项目:corpushash 作者: NAMD 项目源码 文件源码
def hash_token(token, hash_function='sha256', salt_length=32, salt=None):
    """
    takes a token and hashes it along with a random salt of given length, 
    according to the specified hash function.
    :param token: str: string of any length
    :param hash_function: str: hash function to use (check hashlib library).
    :param salt_length: int: salt length in bytes.
    :param salt: bytes: only used for tests, specifies which salt to use.
    :return: str, bytes: hashed token (base85-decoded) and the random salt used.
    """
    if salt is None:
        salt = os.urandom(salt_length)
    token_hasher = hashlib.new(hash_function)
    token_hasher.update(token.encode() + salt)
    token_digest = token_hasher.digest()
    token_base85 = base64.b85encode(token_digest)
    hashed_token = token_base85.decode()
    return hashed_token, salt
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号