连接字符串/从文件中获取值的问题

我一直在编写一个以哈希形式保存密码的程序,但我试图从我的文件中获取一个值,该值存储盐的值。出于某种原因,它似乎不起作用。这是我的代码:


hashpass = hashlib.sha256()

salt = ['hbjGVY0Kj07,kbjgvhjb,ZsGhnBi0lp]


for line in login:

            usr = input()

            pas = input()

            log = line.split(',')

            if usr in line:

                x = line

                salt_num = int(x[2])

                setpass = str(pas + salt[salt_num])

                hashpass.update(setpass.encode('utf-8'))

我已经尝试了一切,但是当我连接字符串时仍然没有结果,我只是得到了 pas 的值


慕莱坞森
浏览 103回答 2
2回答

红糖糍粑

我已经解决了这个问题,但是在比较变量hashpass和log[1]时遇到不同的错误,当比较我的程序声称密码错误时,这是整个程序供参考。login = open('login.csv','r')def logging():&nbsp; &nbsp; atmptcount = 0&nbsp; &nbsp; while atmptcount < 3:&nbsp; &nbsp; &nbsp; &nbsp; usr = input('Please enter your username: ')&nbsp; &nbsp; &nbsp; &nbsp; pas = input('Please enter your password: ')&nbsp; &nbsp; &nbsp; &nbsp; hashpass = hashlib.sha256()&nbsp; &nbsp; &nbsp; &nbsp; for line in login:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log = line.split(',')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if usr.upper() in line:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(log[2])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; salt_num = int(log[2])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setpass = str(pas + salt[salt_num])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashpass.update(setpass.encode('utf-8'))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if usr == log[0] and hashpass.hexdigest() == log[1]:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print('correct')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return True&nbsp; &nbsp; &nbsp; &nbsp; print(hashpass.hexdigest())&nbsp; &nbsp; &nbsp; &nbsp; print(log[1])&nbsp; &nbsp; &nbsp; &nbsp; atmptcount = atmptcount + 1&nbsp; &nbsp; &nbsp; &nbsp; print('Sorry you have entered your details incorrectly, please try again')&nbsp; &nbsp; &nbsp; &nbsp; login.seek(0)&nbsp; &nbsp; print('Sorry, you have reached your maximum login attempts!')&nbsp; &nbsp; return False我稍微更改了变量名,但它是 saem 概念

慕斯王

这是我尝试过的,它有效。您共享的代码有一些问题,我会要求您与原始代码进行交叉检查。import hashlibhashpass = hashlib.sha256()salt = ['hbjGVY0Kj07','kbjgvhjb','ZsGhnBi0lp']login = ["user,68a782faf939dfa370345934d255101926b7f59b3a65ab7db5b0bc6f78ec25e5,0"]for line in login:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #print(line)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; usr = input()&nbsp; &nbsp; &nbsp;# I input "user"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pas = input()&nbsp; &nbsp; &nbsp;# I input "qwerty"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log = line.split(',')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; #print(log)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if usr in line:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = log&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; salt_num = int(x[2])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setpass = str(pas + salt[salt_num])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print(setpass)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hashpass.update(setpass.encode('utf-8'))OUTPUT --> qwertyhbjGVY0Kj07我建议你检查的事情:列表中的所有项目salt都用引号引起来,即字符串。Login 是一个字符串列表,其中包含带有逗号分隔值的元素,就像我创建的那样。换x=line到x=log里面if condition。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python