【发布时间】:2018-06-16 00:22:18
【问题描述】:
我正在使用 python 3,我需要知道如何在 python 3 中对变量而不是字符串使用散列。
我的例子; 这是我目前正在尝试使用的代码,但它不起作用。
foundpassencypt = hashlib.md5(b(pwd))
print(foundpassencypt.hexdigest())
pwd 是我之前在程序中输入的字符串。
pwd = "Password"
我知道如果它是一个字符串,它会像这样布局;
foundpassencypt = hashlib.md5(b"Password")
print(foundpassencypt.hexdigest())
这是完整的代码(它使用 Python3、SQL Lite 和 Appjar)(“Else:”在我发布代码时不合适,在我的代码中是正确的)
else:
usr = login.getEntry("Username")
pwd = login.getEntry("Password") #collects entry of password & username
conn = sqlite3.connect("uHubDatabase.db")
cursor = conn.cursor() #connects to database
find_user=("SELECT Username FROM UserTable WHERE Username = ?") #sets the finding of the username from the database as a varaible
cursor.execute(find_user,[(usr)])
founduser = str(cursor.fetchall())
print(founduser)
removechars = "'(),[]" #Avoids the error of special characters caused by the database outputting strings (Text)
for char in removechars:
founduser = founduser.replace(char,'')
find_pass=("SELECT Password FROM UserTable WHERE Password = ?") #sets the finding of the password from the database as a varaible
cursor.execute(find_pass,[(pwd)])
foundpass = str(cursor.fetchall())
print(foundpass)
removechars = "'(),[]" #Avoids the error of special characters caused by the database outputting strings (Text)
for char in removechars:
foundpass = foundpass.replace(char,'')
pwdencypt = hashlib.md5(pwd) #makes the encypted password using md5 hashing
print(pwdencypt.hexdigest()) # checks the string for comparison
print(founduser)
print(usr)
print(foundpass)
print(pwd)
if founduser == usr and foundpass == pwdencypt: # If correct
print("SUCESS")
login.stop()
home.go()
else: #if incorrect
print("FAIL")
login.retryBox("INCORRECT LOGIN", "The Username or Password entered are incorrect. Please try again.", parent=login)
print("User:", usr, "Pass:", pwd)
conn.close() #closes connection
【问题讨论】:
-
不要使用 MD5 来散列密码。不要只使用单轮任何散列来散列密码。请使用具有足够高迭代次数/工作因子的 BCrypt、SCrypt、PBKDF2 或 Argon2 来散列密码。
标签: python-3.x variables hash passwords md5