day28 hashlib

# 内置方法
# dic = {'k':'v'}
# class Foo:
#     def __init__(self,name,age,sex):
#         self.name = name
#         self.age = age
#         self.sex = sex
#
#     def __getitem__(self, item):
#         if hasattr(self,item):
#             return self.__dict__[item]
#
#     def __setitem__(self, key, value):
#         self.__dict__[key] = value
#
#     def __delitem__(self, key):
#         del self.__dict__[key]
#
# f = Foo('wjj',38,'男')
# print(f['name'])
# f['hobby'] = '男'
# print(f.hobby,f['hobby'])
# del f.hobby
# del f['hobby']
# print(f.__dict__)

# __new__ 构造方法:创建一个对象
# class A:
#     def __init__(self):
#         self.x = 1
#         print('in init.')
#
#     def __new__(cls, *args, **kwargs):
#         print('in new')
#         return object.__new__(A)
# a1 = A()
# a2 = A()
# a3 = A()
# print(a1)
# print(a2)
# print(a3)
# print(a1.x)

#单例模式
# class A:
#     __instance = False
#
#     def __init__(self,name,age):
#         self.name = name
#         self.age = age
#     def __new__(cls, *args, **kwargs):
#         if cls.__instance:
#             return cls.__instance
#         cls.__instance = object.__new__(cls)
#         return cls.__instance
# wjj = A('wjj',38)
# wjj.cloth = '小花袄'
# wjq = A('wjq',11)
# print(wjq)
# print(wjq.__dict__)
# print(wjj)

# __eq__方法
# class A:
#     def __init__(self,name):
#         self.name = name
#
#     def __eq__(self, other):
#         if self.__dict__ == other.__dict__:
#             return True
#         else:
#             return False
# obj1 = A("egg")
# obj2 = A("egg")
# print(obj1 == obj2)

#hash()方法
# class A:
#     def __init__(self,name,sex):
#         self.name= name
#         self.sex = sex
#     def __hash__(self):
#         return hash(self.name + self.sex)
# a = A('egon','男')
# b = A('egv','女')
# print(hash(a))
# print(hash(b))

#hashlib模块

# import hashlib
# md5 = hashlib.md5()
# md5.update(b'15254')
# print(md5.hexdigest())
# md5 = hashlib.md5(bytes('盐',encoding='utf-8'))
# print(md5.hexdigest())

#简单登陆验证

import hashlib
user = input('username:')
pwd = input('pwd:')
with open('userinfo') as f:
    for line in f:
        use,passwd,role = line.split('|')
        md5 = hashlib.md5()#可在这里加盐
        md5.update(bytes(pwd,encoding = 'utf-8'))
        md5_pwd = md5.hexdigest()
        if use == user and md5_pwd == passwd:
            print('登陆成功')

猜你喜欢

转载自www.cnblogs.com/wujunjie-sir/p/9281106.html