hashlib模块-加密的模块,加盐

1、MD5加密

 md5加密是不可逆的

    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))

2、加盐

就是在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)

3、其他加密方式

1 password='adsf%&&^*'
2 m = hashlib.sha224(password.encode()) #长度长了,也是不可以解密的
3 print(m.hexdigest())
4 
5 m = hashlib.sha256(password.encode())
6 print(m.hexdigest())
备注:撞库:加密的密文存入数据,然后找相应的匹配,复杂的密码解不出来

原文地址:https://www.cnblogs.com/once-again/p/9735944.html