【问题标题】:Hashing a variable in python 3在python 3中散列一个变量
【发布时间】: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


【解决方案1】:

你不需要b()

import hashlib
pwd = "Password"
foundpassencypt = hashlib.md5(pwd.encode('utf-8'))
print(foundpassencypt.hexdigest()) 

输出:

dc647eb65e6711e155375218212b3964

UPD。散列算法不支持 unicode,因此您必须对 sring 进行编码。详情见pythonissue2948

【讨论】:

  • TypeError: Unicode 对象必须在散列之前进行编码
  • 那么你需要对它进行编码,因为哈希算法不支持unicode。你仍然不需要b
  • 我如何“将其编码为哈希算法不支持 unicode。”?
  • 我已将其包含在上面的代码中,pwd.encode('utf-8') 部分
  • 另一个答案也是如此
【解决方案2】:

在散列一个字符串变量之前,你应该先对其进行编码。

例子:

a = "123321"
print(hashlib.md5(a.encode('utf-8')).hexdigest())

【讨论】:

  • 这给了我一个错误AttributeError: 'bytes' object has no attribute 'hexdigest'
【解决方案3】:

不要将 MD5 用于散列密码

由于多种原因,它非常不安全,其中最重要的是任何哈希的单次迭代都不够,另一个是现在可以生成 MD5 冲突(并且已经能够生成多年)。

使用具有高迭代次数/工作系数的 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 秒来散列一个密码开始(这将只使用系统上的一个核心,因此多核系统可以同时处理多个密码)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 2011-07-23
    • 2015-08-30
    • 2021-07-22
    相关资源
    最近更新 更多