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
评论列表
文章目录