散列 python 中的变量 3
Hashing a variable in python 3
我正在使用 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:" 在我 post 时不合适代码,在我的代码中是正确的)
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
你不需要b()
import hashlib
pwd = "Password"
foundpassencypt = hashlib.md5(pwd.encode('utf-8'))
print(foundpassencypt.hexdigest())
输出:
dc647eb65e6711e155375218212b3964
更新。散列算法不支持 unicode,因此您必须对 sring 进行编码。有关详细信息,请参阅 python issue2948
在散列字符串变量之前,您应该先对其进行编码。
示例:
a = "123321"
print(hashlib.md5(a.encode('utf-8')).hexdigest())
不要使用 MD5 散列密码
出于多种原因,它非常不安全,其中最重要的是任何哈希的单次迭代是不够的,另一个是现在可以生成 MD5 冲突(并且已经能够生成多年)。
使用具有高迭代 count/work 因子的 PBKDF2、BCrypt、SCrypt 或 Argon2 来散列密码。
请注意,我在 my Github repository 中确实有一个粗糙但实用的 Python 2.7 PBKDF2 示例。一个调用的例子是:
BinaryOutput = pbkdf2_math.pbkdf2_bin(args.password, args.salt, args.iterations, args.outputBytes, hashlib.sha512)
或者,使用 passlib for Python 2 and 3 调用 PBKDF2、BCrypt、SCrypt 或 Argon2。
在所有情况下,使用足够高的迭代次数或工作因子;开始,也许,用 1/10 或 1/100 秒来散列一个密码(这将只使用你系统上的一个核心,所以多核系统可以一次处理多个密码)。
我正在使用 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:" 在我 post 时不合适代码,在我的代码中是正确的)
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
你不需要b()
import hashlib
pwd = "Password"
foundpassencypt = hashlib.md5(pwd.encode('utf-8'))
print(foundpassencypt.hexdigest())
输出:
dc647eb65e6711e155375218212b3964
更新。散列算法不支持 unicode,因此您必须对 sring 进行编码。有关详细信息,请参阅 python issue2948
在散列字符串变量之前,您应该先对其进行编码。
示例:
a = "123321"
print(hashlib.md5(a.encode('utf-8')).hexdigest())
不要使用 MD5 散列密码
出于多种原因,它非常不安全,其中最重要的是任何哈希的单次迭代是不够的,另一个是现在可以生成 MD5 冲突(并且已经能够生成多年)。
使用具有高迭代 count/work 因子的 PBKDF2、BCrypt、SCrypt 或 Argon2 来散列密码。
请注意,我在 my Github repository 中确实有一个粗糙但实用的 Python 2.7 PBKDF2 示例。一个调用的例子是:
BinaryOutput = pbkdf2_math.pbkdf2_bin(args.password, args.salt, args.iterations, args.outputBytes, hashlib.sha512)
或者,使用 passlib for Python 2 and 3 调用 PBKDF2、BCrypt、SCrypt 或 Argon2。
在所有情况下,使用足够高的迭代次数或工作因子;开始,也许,用 1/10 或 1/100 秒来散列一个密码(这将只使用你系统上的一个核心,所以多核系统可以一次处理多个密码)。