def hash_password(password, encoding=HashEncoding.BINARY, salt_size=SALT_SIZE):
"""
Returns a hashed password.
"""
salt = urandom(salt_size)
hasher = hashlib.new(config.get('security.hash_algorithm') or 'sha256')
if isinstance(password, text_type):
password = password.encode('utf-8')
hasher.update(password)
hasher.update(salt)
if encoding == HashEncoding.BINARY:
return salt + hasher.digest()
elif encoding == HashEncoding.HEX:
return bytes_to_native_str(hexlify(salt)) + hasher.hexdigest()
elif encoding == HashEncoding.BASE64:
return bytes_to_native_str(
urlsafe_b64encode(salt) + urlsafe_b64encode(hasher.digest()))
raise ValueError()
评论列表
文章目录