首页 > 解决方案 > 在密码更改中限制相同的密码

问题描述

我正在使用 tkinter 在 python 上构建一个 gui,我已经登录、注册和更改密码屏幕,将数据托管在 mysql 上,我想设置一个选项,如果用户已经选择了密码“x”,例如,他将只能再重复一次密码,之后他将无法在更改密码屏幕上选择并重复相同的密码,有什么线索吗?

标签: pythonpython-3.xtkinter

解决方案


在另一个表中跟踪用户的盐+哈希组合,并使用旧盐计算他们的新哈希,以确保他们以前从未尝试过使用它。

一个例子...

假设您的用户的密码历史记录在字典列表中:


password_history = [
  {
    "salt": "89!$@sg",
    "hash": "asdfjhlaksjdhflkjahsdlkfjh",
  },
]

def has_used_password(password_history, new_password):
  hashes = set(h["hash"] for h in password_history)

  count = 0
  for entry in password_history:
    hash_with_old_salt = hash_password(new_password, entry["salt"])
    if hash_with_old_salt in hashes :
      count += 1
  return count

推荐阅读