fork download
  1. import hmac, random
  2.  
  3. def hmac_md5(key, s):
  4. return hmac.new(key.encode('utf-8'), s.encode('utf-8'), 'MD5').hexdigest()
  5.  
  6. class User(object):
  7. def __init__(self, username, password):
  8. self.username = username
  9. self.key = ''.join([chr(random.randint(48, 122)) for i in range(20)])
  10. self.password = hmac_md5(self.key, password)
  11.  
  12. db = {
  13. 'michael': User('michael', '123456'),
  14. 'bob': User('bob', 'abc999'),
  15. 'alice': User('alice', 'alice2008')
  16. }
  17.  
  18. def login(username, password):
  19. user = db[username]
  20. return user.password == hmac_md5(user.key, password)
  21.  
  22. # 测试:
  23. assert login('michael', '123456')
  24. assert login('bob', 'abc999')
  25. assert login('alice', 'alice2008')
  26. assert not login('michael', '1234567')
  27. assert not login('bob', '123456')
  28. assert not login('alice', 'Alice2008')
  29. print('ok')
  30.  
  31. # your code goes here
Success #stdin #stdout 0.1s 17704KB
stdin
Standard input is empty
stdout
ok