您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python最安全的方法来从数据库存储和检索密码

Python最安全的方法来从数据库存储和检索密码

将密码+盐存储为哈希和盐。看一下Django的工作方式:基本文档源代码。它们在数据库中存储<type of hash>$<salt>$<hash>在单个char字段中。您也可以将这三个部分存储在单独的字段中。

设置密码的功能

def set_password(self, raw_password):
    import random
    algo = 'sha1'
    salt = get_hexdigest(algo, str(random.random()), str(random.random()))[:5]
    hsh = get_hexdigest(algo, salt, raw_password)
    self.password = '%s$%s$%s' % (algo, salt, hsh)

get_hexdigest只是一些哈希算法的瘦包装。您可以为此使用hashlib。就像是hashlib.sha1('%s%s' % (salt, hash)).hexdigest()

并检查密码的功能

def check_password(raw_password, enc_password):
    """
    Returns a boolean of whether the raw_password was correct. Handles
    encryption formats behind the scenes.
    """
    algo, salt, hsh = enc_password.split('$')
    return hsh == get_hexdigest(algo, salt, raw_password)
python 2022/1/1 18:44:15 有305人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶