Discuss / Python / 交作业

交作业

Topic source
import sys
import hashlib


db = {}

def get_md5(password):
    md5 = hashlib.md5()
    md5.update(password.encode('utf-8'))
    password = md5.hexdigest()
    return password

def login(user,password):
    global db
    if user in db and db[user] == get_md5(password + username + 'the-Salt'):
        return True
    else:
        return False

def register(username,password):
    global db
    if username in db:
        print('用户名已存在!')
        exit()
    else:
        db[username] = get_md5(password + username + 'the-Salt')
        print('恭喜您,注册成功!')

if __name__ == '__main__':
    print('--------------注册------------')
    username = input('请输入用户: ')
    password = input('请输入密码: ')
    register(username,password)
    print('--------------登录------------')
    username = input('请输入用户: ')
    password = input('请输入密码: ')
    if login(username,password):
        print('登录成功!')
    else:
        print('用户名或密码错误!')

  • 1

Reply