#coding:gbk
import hashlib, random
db = {}
#注册
def register(username, password):
db[username] = User(username, password)
#加密
def get_md5(s):
return hashlib.md5(s.encode('utf-8')).hexdigest()
class User(object):
def __init__(self, username, password):
self.username = username
self.salt = ''.join([chr(random.randint(48, 122)) for i in range(20)])
self.password = get_md5(password+username+self.salt)
#登陆
def login(username, password):
user = db[username]
if user.password == get_md5(password+username+user.salt):
print('正确')
else:
print('密码错误')
register('bob', '12345')
register('boc', '123456')
login('boc', '123456')
#coding:gbk
import hmac, random
db = {}
def get_md5(ke, password):
return hmac.new(ke.encode('utf-8'), password.encode('utf-8'), digestmod='MD5').hexdigest()
class User:
def __init__(self, username, password):
self.username = username
self.key = ''.join([chr(random.randint(48, 122)) for i in range(20)]) #运行20次
self.password = get_md5(self.key, password)
def register(username, password):
db[username] = User(username, password)
s = db[username]
print(s.password)
def login(username, password):
user = db[username]
if user.password == get_md5(user.key, password):
print('允许登陆')
else:
print('密码错误')
register('bob', '12345')
register('boc', '123456')
login('bob', '12345')
login('boc', '123456')