1.用途:
# 文件校验
# 文件是否被改变
# 登录密码
#不能解密,但可以“撞库”
#加盐 hashlib.md5('nezha'.encode('utf-8'))
2.
import hashlib
md5_obj = hashlib.md5('nezha'.encode('utf-8'))
md5_obj.update(''.encode('utf-8'))
print(md5_obj.hexdigest())
md5_obj.update('hello,egon~'.encode('utf-8'))
print(md5_obj.hexdigest())
# user = 'alex'
# pwd = '3713'
# md5_obj = hashlib.md5(user.encode('utf-8'))
# md5_obj.update(pwd.encode('utf-8'))
# print(md5_obj.hexdigest())
3.读文件内容
第一种方法:
import hashlib
# md5_obj = hashlib.md5()
# f = open('filename','rb')
# for line in f:
# md5_obj.update(line.encode('utf-8'))
# md5_obj.hexdigest()
第二种方法:
import hashlib
# md5_obj = hashlib.md5()
# import os
# filesize = os.path.getsize('filename')
# f = open('filename','rb')
# while filesize>0:
# if filesize > 1024:
# content = f.read(1024)
# filesize -= 1024
# else:
# content = f.read(filesize)
# filesize -= filesize
# md5_obj.update(content)
# md5_obj.hexdigest()
4.主要掌握MD5, 还有一个SHA