Discuss / Python / 作业12

作业12

Topic source

孤o赦免你

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

def hmac_md5(key, s):

    bkey = key.encode('utf-8').decode('utf-8')

    bs = s.encode('utf-8').decode('utf-8')

    return hmac.new(bkey.encode('utf-8'), bs.encode('utf-8'), 'MD5').hexdigest()

class User(object):

    def __init__(self, username, password):

        self.username = username

        self.key = ''.join([chr(random.randint(48, 122)) for i in range(20)])

        self.password = hmac_md5(self.key, password)

db = {

    'michael': User('michael', '123456'),

    'bob': User('bob', 'abc999'),

    'alice': User('alice', 'alice2008')

}

def login(username, password):

    user = db[username]

    return user.password == hmac_md5(user.key, password)


  • 1

Reply