Python的hashlib产生不一致的结果

2024-10-01 02:26:14 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我试图编写一个程序,通过创建一个.txt文件来创建一个帐户,这个文件是用户名,然后用md5散列密码并将其放入文件中。但是,每当我跑步

hashlib.md5(pasw.encode)

它会产生奇怪的不一致的结果,不管它是否在函数中编码它都会有所不同。上次我测试它时,它似乎在第一次运行时产生了一个随机哈希。现在,在新项目中进行测试时,有时会为不同的字符串生成相同的哈希。我想尽一切办法都试过了,但一无所获。你可以看到我尝试过的一些事情。 提前感谢你的帮助,对不起,如果我的代码看起来很糟糕,我做这个项目是为了学习。以下是完整的代码,如果有些代码不相关,请抱歉:

import hashlib

# Creates an account
def create():
    print('Create an account')
    user = input('Please enter a username: ')
    pasw = input('Please enter a password: ')
    usef = user+'.txt'
    f = open(usef,'w')
    # hash'd it
    hash_object = str(hashlib.md5(pasw.encode()))
    # Printing hash for debug
    print(hash_object)
    # This doesn't help but I tried
    hash_object = str(hashlib.md5(pasw.encode()))
    # Printing hash for debug
    print(hash_object)
    f.write(hash_object)
    f.close()

# Logs into a pre-existing account
def login():
    print('Welcome! To log in:')
    user = input('Please enter a username: ')
    pasw = input('Please enter a password: ')
    usef = user+'.txt'
    # Comparative hash
    hash_object = str(hashlib.md5(pasw.encode()))
    # Printing hash for debug
    print(hash_object)
    # This doesn't help but I tried
    hash_object = str(hashlib.md5(pasw.encode()))
    # Printing hash for debug
    print(hash_object)
    f = open(usef,'r')
    # Checks to see if it's right
    if hash_object == f:
        print('Welcome, '+user+'!')
        return(user)
    else:
        print('User/password not found')
        return(0)

# Changes a password for a user
def change(user):
    while True:
        pasw = input('Please enter your current password: ')
        usef = user+'.txt'
        hash_object = str(hashlib.md5(pasw.encode()))
        # Printing hash for debug
        print(hash_object)
        # This doesn't help but I tried
        hash_object = str(hashlib.md5(pasw.encode()))
        f = open(usef,'r')
        if hash_object == f:
            pasw = input('Please enter your new password: ')
            past = input('Please repeat your new password: ')
            while True:
                if pasw == past:
                    usef = p+'.txt'
                    f = open(usef,'w')
                    hash_object = str(hashlib.md5(pasw.encode()))
                    # Printing hash for debug
                    print(hash_object)
                    # This doesn't help but I tried
                    hash_object = str(hashlib.md5(pasw.encode()))
                    f.write(hash_object)
                    f.close()
                    print('Done!')
                    return()
                else:
                    print('They aren\'t the same')
                    pass
        else:
            print('That doesn\'t match the password on the system')

# Help with commands
def halp():
    print('You can type "Create" to create an account')
    print('You can type "Login" to log into a pre-existing account')
    print('More functionality will be added')
    print('Maybe')

# =============== Main Body of Code ==================

while True:
    print('What do you want to do?')
    print('Type "Help" for commands')
    x = input()
    x = x.upper()
    if x == 'CREATE':
        create()
    elif x == 'LOGIN':
        p = login()
        if p != 0:
            while True:
                print('What would you like to do now?')
                print('"Logout" to log out')
                print('or "Change password"')
                print('I think you can guess that')
                x = input()
                x = x.upper()
                if x == 'CHANGE PASSWORD':
                    change(p)
    elif x == 'HELP':
        halp()
    elif x == 'DEV':
        y = input()
        print(str(hashlib.md5(y.encode())))
        # This doesn't help but I tried
        print(str(hashlib.md5(y.encode())))
    else:
        print("Command not found")
        print("There are four commands")
        print("It's not that hard")

编辑:哦,我现在明白了。谢谢你的帮助!在


Tags: forinputifobjectpasswordhashmd5encode