【问题标题】:Hashed identical strings aren't the same when hashed twice哈希相同的字符串在哈希两次时不相同
【发布时间】:2021-09-21 05:43:54
【问题描述】:

我有一个登录程序,它对字符串进行哈希处理并将其存储在一个文件中以创建一个新帐户。当我需要登录时,登录详细信息字符串会被散列,程序会检查散列字符串是否在文件中匹配。该程序无需散列即可工作,但是当我散列相同的登录详细信息时,散列值不一样。我检查过,字符串完全一样。这是我的代码:

import tkinter
import math
import os
import hashlib

# The login function #
def login(username, password, file_path):
    file_new = open(file_path, "a")
    file_new.close()

    file = open(file_path, "r")
    file_content = file.read()
    print(file_content)
    file.close()

    hashed_username = hashlib.md5(bytes(username, "utf-8"))
    hashed_password = hashlib.md5(bytes(password, "utf-8"))
    print(f"Hashed username: {hashed_username}, hashed password: {hashed_password}")

    if f"{hashed_username},{hashed_password}" in file_content[:]:
        return "You were logged in successfully"
    else:
        return "We could not find your account. Please check your spelling and try again."

# The account creation function #
def newaccount(username, password, file_path):
    file_new = open(file_path, "a")
    file_new.close()

    # Reading the file #
    file = open(file_path, "r")
    file_content = file.read()
    print(file_content)
    file.close()

    # Hashing the account details #
    hashed_username = hashlib.md5(bytes(username, "utf-8"))
    hashed_password = hashlib.md5(bytes(password, "utf-8"))
    print(f"Hashed username: {hashed_username}, hashed password: {hashed_password}")
    
    file_append = open(file_path, "a")

    # Checking to see if the details exist in the file #
    if f"{hashed_username},{hashed_password}" in file_content[:]:
        file_append.close()
        return "You already have an account, and were logged in successfully"
    else:
        # Writing the hashed details to the file #
        file_append.write(f"{hashed_username},{hashed_password}\n")
        file_append.close()
        return "New account created."        

logins_path = "Random Scripts\Login Program\logins.txt"

signin_message = input("Would you like to: \n1. Create an account \nor \n2. Log in\n")
if signin_message == "1":
    print("User chose to create account")
    newacc_username = input("Input a username: ")
    newacc_password = input("Input a password: ")
    print(newaccount(newacc_username, newacc_password, logins_path))
elif signin_message == "2":
    print("User chose to log in")
    username = input("Input your username: ")
    password = input("Input your password: ")
    print(login(username, password,logins_path))
else:
    print("Please enter 1 or 2")

【问题讨论】:

  • 输入?输出?预期输出?
  • @thebjorn 输入将是用户名和密码的两个字符串。这些字符串的用途根据用户是选择创建帐户还是登录帐户而有所不同。假设提供了原始帐户详细信息,预期的输出是“您已成功登录”。我得到的输出是程序无法找到我的帐户。我打印了从用于登录的字符串中获得的哈希值,它们与我最初对帐户详细信息进行哈希处理时写入文本文件的哈希值不匹配。
  • 编辑你的问题:去掉所有有效的东西,留下尽可能小的无效代码。提供您用来确定它不起作用的输入。提供你得到的输出。向我们展示为什么您认为您得到的输出不是您所期望的。 tl;博士告诉我们,不要告诉我们。\

标签: python hash hashlib


【解决方案1】:
hashed_username = hashlib.md5(bytes(username, "utf-8"))

这个函数返回一个散列对象,当你打印它或者把它写入一个文件时,你会得到这样的东西:

<md5 HASH object @ 0x7f8274221990>

...这不是非常有用。

如果您想要哈希的实际文本,请致电.hexdigest()

hashed_username = hashlib.md5(bytes(username, "utf-8")).hexdigest()
# returns e.g. "47bce5c74f589f4867dbd57e9ca9f808"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 2018-07-28
    • 1970-01-01
    • 2020-07-16
    • 2019-10-30
    相关资源
    最近更新 更多