hashlib练习
练习一
练习二
练习三
答案
#!/usr/bin/python# #-*-coding:UTF-8-*- import hashlib ''' 字典存用户名和密码 ''' db = { 'michael':'c4ca4238a0b923820dcc509a6f75849b', 'bob':'c81e728d9d4c2f636f067f89cc14862c', 'alice':'eccbc87e4b5ce2fe28308fd9f2a7baf3' } ''' 用户密码加密 ''' def calc_md5(password): md5 = hashlib.md5() md5.update(password) passwd = md5.hexdigest() return passwd ''' 用户注册时加盐 ''' def register(username,password): pas = username + password + 'the-Salt' print pas db[username] = calc_md5(pas) return calc_md5(pas) ''' 用户登录 ''' def login(user,password): if db[user] == register(user,password): print 'True' else: print 'False' #用户注册 register('tom','t123') #用户登录 login('tom','t123')