md5加密是不可逆的python
print(dir(m)) #把变量的方法打印出来安全
hashlib.md5:加密
xx.hexdigest():返回密文
xx.encode:将字符串转成二进制的,转成二进制才能加密
1 import hashlib 2 # import md5 #python2里面的 3 password = '123456kjfskj' 4 # print(password.encode()) #转成二进制才能够加密 5 m = hashlib.md5(password.encode()) #加密。md5加密是不可逆的 6 # print(m) 7 print(m.hexdigest()) #加密以后获得的是32位的字符串,打印密文 8 # print(dir(m))
就是在hashlib模块中的md5加密方法时,传入一个你本身想给的盐,或者干脆随机生成(比较安全,将盐封装在类中)函数
1 import hashlib 2 def my_md5(s:str,salt): #传入密码和盐 3 #加密的函数 4 #若是传入盐的参数,则加盐 5 s = str(s) #类型转换,转成字符串 6 if salt: 7 s = s + salt 8 m = hashlib.md5(s.encode()) #md5加密 9 return m.hexdigest() #返回密文 10 res = my_md5('123456','akdjkf') 11 print(res)
1 password='adsf%&&^*' 2 m = hashlib.sha224(password.encode()) #长度长了,也是不能够解密的 3 print(m.hexdigest()) 4 5 m = hashlib.sha256(password.encode()) 6 print(m.hexdigest())
备注:撞库:加密的密文存入数据,而后找相应的匹配,复杂的密码解不出来