Discuss / Python / 加盐版

加盐版

Topic source

岡崎鏡

#1 Created at ... [Delete] [Delete and Lock User]
import hashlib

db = {}

def calc_md5(username, password):
    md5 = hashlib.md5()
    password += username + 'the-Salt'
    md5.update(password.encode('utf-8'))
    return md5.hexdigest()

def register():
    global db

    username = input('Input username: ')
    password = input('Input password: ')
    db[username] = calc_md5(username, password)
    print('User ' + username + ' registered. Now you can try to login.')

def login():
    global db

    username = input('Input username: ')
    password = input('Input password: ')
    if db[username] == calc_md5(username, password):
        print('Login Succeed.')
    else:
        print('Login failed.')

def main():
    while True:
        select = input('\nSelect:\n1. Register\n2. Login\n\nYour Choice: ')
        if select == '1':
            register()
        elif select == '2':
            login()

if __name__ == '__main__': main()

  • 1

Reply