Discuss / Python / work 2333

work 2333

Topic source

Phioton

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

1

def login(user, password):
    md5_new = hashlib.md5()
    md5_new.update(str(password).encode('utf-8'))
    return md5_new.hexdigest()==db[str(user)]

2

# -*- coding: utf-8 -*-
import hashlib, random

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 + 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 == get_md5(password+user.salt)

初学者总是分不清变量和字符串的区别,谢谢谢谢

第一个例子不需要str(password)

你输入的password本身已经是字符串了,当然str一下也无妨


  • 1

Reply