#coding:utf-8
#python 检测文件MD5值
#python version 2.6
import hashlib
import os
#简单的测试一个字符串的MD5值
src = ‘teststring’
print (hashlib.md5(src).hexdigest().upper())
#hexdigest() 为十六进制值,digest()为二进制值
#使用update
m0=hashlib.md5()
m0.update(src)
print m0.hexdigest().upper()
#一个小文件的MD5值
filename = ‘c:boot.ini’
f = file(filename,’rb’)
m1 = hashlib.md5()
m1.update(f.read(8096))
print m1.hexdigest().upper()
f.close()
#大文件的M
unix 下md5sum的python版本
#!/usr/bin/python
#encoding=utf-8
import io
import sys
import hashlib
import string
def printUsage():
print(””’Usage: [python] pymd5sum.py <filename>”’)
def main():
if(sys.argv.__len__()== 2):
#print(sys.argv[1])
m = hashlib.md5()
file = io.FileIO(sys.argv[1],’r’)
bytes = file.read()
while(bytes != b”):
m.update(bytes)
bytes = file.read()
file.close()
md5value = m.hexdigest()
print(md5value+”t”+sys.argv[1])
else:
printUsage()
if __name__ == ‘__main__’:
main()