2 回答

TA貢獻1815條經驗 獲得超6個贊
我已經解決了這個問題,但是在比較變量hashpass和log[1]時遇到不同的錯誤,當比較我的程序聲稱密碼錯誤時,這是整個程序供參考。
login = open('login.csv','r')
def logging():
atmptcount = 0
while atmptcount < 3:
usr = input('Please enter your username: ')
pas = input('Please enter your password: ')
hashpass = hashlib.sha256()
for line in login:
log = line.split(',')
if usr.upper() in line:
print(log[2])
salt_num = int(log[2])
setpass = str(pas + salt[salt_num])
hashpass.update(setpass.encode('utf-8'))
if usr == log[0] and hashpass.hexdigest() == log[1]:
print('correct')
return True
print(hashpass.hexdigest())
print(log[1])
atmptcount = atmptcount + 1
print('Sorry you have entered your details incorrectly, please try again')
login.seek(0)
print('Sorry, you have reached your maximum login attempts!')
return False
我稍微更改了變量名,但它是 saem 概念

TA貢獻1864條經驗 獲得超2個贊
這是我嘗試過的,它有效。您共享的代碼有一些問題,我會要求您與原始代碼進行交叉檢查。
import hashlib
hashpass = hashlib.sha256()
salt = ['hbjGVY0Kj07','kbjgvhjb','ZsGhnBi0lp']
login = ["user,68a782faf939dfa370345934d255101926b7f59b3a65ab7db5b0bc6f78ec25e5,0"]
for line in login:
#print(line)
usr = input() # I input "user"
pas = input() # I input "qwerty"
log = line.split(',')
#print(log)
if usr in line:
x = log
salt_num = int(x[2])
setpass = str(pas + salt[salt_num])
print(setpass)
hashpass.update(setpass.encode('utf-8'))
OUTPUT --> qwertyhbjGVY0Kj07
我建議你檢查的事情:
列表中的所有項目
salt
都用引號引起來,即字符串。Login 是一個字符串列表,其中包含帶有逗號分隔值的元素,就像我創建的那樣。
換
x=line
到x=log
里面if condition
。
添加回答
舉報