【发布时间】:2021-10-23 13:29:59
【问题描述】:
我使用 bcrypt 库存储了一个哈希密码,因此它总是在我的数据库中存储不同的哈希字符串。如果字符串值不一样,如何将它们与商店密码进行比较?
@login.route('/log',methods=['POST'])
def login():
error = None
# get data from JSON
body = request.get_json()
# if data contains something
if body != error:
# Verification of POST method
if request.method == 'POST':
# bucle for empty values findings
validation = all(x != "" for x in body.values())
if validation:
username_mod = body['username']
password_mod = body['password_hash']
forced = b"valentina"
hashed = hashpw(password_mod.encode('utf-8'), gensalt())
userMatch = User.query.filter_by(username=username_mod).first()
store_password = userMatch.password_hash
if checkpw(forced, hashed):
print("it matches")
else:
print("they dont")
if userMatch:
if checkpw(hashed, store_password):
pswd_match = True
else:
pswd_match = False
if userMatch and pswd_match:
return msg_handler("user allowed", 200)
else:
return msg_handler("user denied", 400)
else:
return msg_handler("missing value in 1 or more parameters", 400)
else:
return msg_handler("Must be POST method", 400)
else:
return msg_handler("no data", 400)
【问题讨论】:
-
加密输入密码并与db中的密码比较,尝试
encrypt_pwd('pwd-goes-here') == 'encrypted-pwd-string-goes=here -
它们是不同的,因为 bcrypt 库总是更改哈希值。我的密码已经存储,然后我得到一个新字符串,我知道它与存储的值相同。