Discuss / Python / 模拟注册登录验证

模拟注册登录验证

Topic source

反草

#1 Created at ... [Delete] [Delete and Lock User]
import hashlib
db={}
salt='haochi'

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

def register(username, password):
    if db.get(username):
        print('username already has existed')
    else:
        db[username] = get_md5(password + username + salt)
        print('register success')

def login(username, password):
    if db.get(username):
        if db[username]== get_md5(password + username + salt):
            print('login success')
        else:
            print('login fail')
    else:
        print('username isn\'t exist')

写的很不错。考虑到了注册和没注册过的情形。很棒


  • 1

Reply