所以我尝试编写一个可以创建帐户的程序,通过创建一个用户名的.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")
编辑:哦,我现在明白了。谢谢你的帮助!