006hashlib模块

hashlib模块

import hashlib
# 密码加密
# 文件的一致性校验
# 加盐就是加在前面
# sha系列: 安全系数高,耗时高.
# ret = hashlib.sha512()
# 分步update 与一次性updete效果一样 所以文件效验可以for循环

import hashlib
new_md5 = hashlib.md5()  #创建hashlib的md5对象
new_md5.update('字符串'.encode(‘utf-8))  #将字符串载入到md5对象中,获得md5算法加密#注意这里必须要编码,否则报错。
print(new_md5.hexdigest())  #通过hexdigest()方法,获得new_md5对象的16进制md5显示。
简单来说。就是三步:
1,建立加密对象。2,对字符串进行算法加密。3,获得16进制显示

# md5
# 加固定盐
ret = hashlib.md5('1'.encode('utf-8'))#1就是盐
# ret.update('23456'.encode('utf-8'))#动态盐就是设置变量
s = ret.hexdigest()
print(s,type(s))

# 文件加密效验
import hashlib
def md5_file(path):
    ret = hashlib.md5()
    with open(path,mode='rb') as f1:
        while 1:
            content = f1.read(1024)
            if content:
                ret.update(content)
            else:
                return ret.hexdigest()
print(md5_file(r'D:s23day17python-3.7.4rc1-embed-win32.zip'))
原文地址:https://www.cnblogs.com/saoqiang/p/12177122.html