password.py 文件源码

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

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


问题


面经


文章

微信
公众号

扫码关注公众号