Discuss / Python / 作业2

作业2

Topic source

Super-String

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


# 计算 MD5 值
def calc_md5(password, salt):
    md5 = hashlib.md5()
    md5.update((password + salt).encode('utf-8'))
    return md5.hexdigest()



# 用户类
class User(object):
    def __init__(self, username, password):
        self.username = username
        self.salt = 'the-Salt'
        self.password = calc_md5(password, self.salt)



# 用户数据库
db = {
    'michael': User('michael', '123456'),
    'bob': User('bob', 'abc999'),
    'alice': User('alice', 'alice2008')
}



# 验证用户登录
def login(username, password):
    user = db[username]
    return user.password == calc_md5(password, user.salt)







  • 1

Reply