我在标题中说过,我希望密码在保存时进行哈希处理。这可能吗?
def __OnClickSaveLoginButton(self):
id = self.idEditLine.GetText()
pwd = self.pwdEditLine.GetText()
if (len(id) == 0 or len(pwd) == 0):
self.PopupNotifyMessage("ID and Password required",self.SetIDEditLineFocus)
return
file_object = open("account.cfg", "w")
file_object.write(id+"\n"+pwd)
self.PopupNotifyMessage("Saved.",self.SetIDEditLineFocus)
file_object.close()
最佳答案
您需要使用 python hashlib 。一个示例可能如下所示:
import hashlib
def valid_password(userid, password):
user = get_user(userid)
pw_hash = hashlib.sha256(password).hexdigest()
if user.hash == pw_hash:
return True
return False
此外,我建议查看此 SO 中提到的一些密码存储最佳实践。
编辑:我在本例中使用了 sh256,但这作为消息摘要更有用。更好的用法是 hashlib.pbkdf2_hmac 或其他 key 派生函数。写得很好here .
关于python - .py 文件中的哈希密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48448830/