Discuss / Python / train2

train2

Topic source

叉烧叉烧

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

import hashlib,random

db = {}

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)

def get_md5(s):

    md5 = hashlib.md5()

    md5.update(s.encode('utf-8'))

    return md5.hexdigest()

def register(username, password):

    db[username] = User(username, password)

def login(username, password):

    user = db[username]

    return user.password == get_md5(password+user.salt)

register('chenjia','123456')

assert login('chenjia','123456')

assert not login('chenjia','543321')

print('ok')


  • 1

Reply