'''
计算md5值
'''
import codecs
import hashlib
import os
# @CyTime.getFunctionRunTime
def getStringMd5(string): #计算字符串的MD5值
md5 = hashlib.md5(string.encode()).hexdigest()
return md5
#计算文件的MD5值:方法一,小文件small
# @CyTime.getFunctionRunTime
def getSmallFileMd5(file):
if os.path.isfile(file):
with codecs.open(file, 'rb') as f:
md5 = hashlib.md5(f.read()).hexdigest()
return md5
# 计算文件的MD5值:方法二,大文件Large,比方法一快
# @CyTime.getFunctionRunTime
def getLargeFileMd5(file):
if os.path.isfile(file):
myhash = hashlib.md5()
with codecs.open(file, 'rb') as f:
while 1:
b = f.read(8096)
if not b:
break
myhash.update(b)
md5 = myhash.hexdigest()
return md5
def demo():
url = "V:\MyVideo\python\Python 正则表达式.docx"
print(getStringMd5(url))
print(getSmallFileMd5(url))
print(getLargeFileMd5(url))
Windows命令查看文件MD5
certutil -hashfile yourfilename.ext MD5
certutil -hashfile yourfilename.ext SHA1
certutil -hashfile yourfilename.ext SHA256
certutil -hashfile data.csv MD5