学了一下hashlib模块的常用部分。
hashlib作用:把一个明文对象转换成加密对象
import hashlib
a=input("输入字符串")
b=hashlib.md5()
"创建md5对象"
b.update(a.encode("utf-8"))
"把输入的对象放入md5对象中,进行转化,注意对象a的编码方式"
print(b.hexdigest()+"\n")
"以16进制输出"
"简写方式"
c=hashlib.sha256("光明王".encode('utf-8')).hexdigest()
print(c)
输入字符串黑暗王
65b077631c854051ac5c4efba3460104
5489cff0702b559ac8aaca9581d23f0fc2e0c6e0ed980e40073ad140012960c9
Process finished with exit code 0
写一个可以存储用户名和加密了的密码的类,并且可以检测输入的密码是否正确
import hashlib
class User:
def __init__(self,user,password):
self.user=user
self.password=self.scrit(password)
"注意self.scrit()"
def scrit(self,password):
"加密函数"
a= hashlib.sha384(password.encode('utf-8')).hexdigest()
return a
def cheak(self,password):
if self.password==self.scrit(password):
print(self.user+"密码输入正确")
else:
print(self.user+"密码输入错误")
a=User("ZHOU","DU")
a.cheak("DU")
ZHOU密码输入正确
Process finished with exit code 0