With the Hashlib module, you can verify file MD5 conformance encryption:
#python detection file MD5 value #python version 2.6 import hashlibimport os,sys #简单的测试一个字符串的MD5值def GetStrMd5 (src): m0=hashlib.md5 () M0.update (src) print m0.hexdigest () pass #大文件的MD5值def GetFileMd5 (filename): If not os.path.isfile (filename): return Myhash = Hashlib.md5 () f = file (filename, ' RB ') while true:b = F.read (8096) if not B: Break Myhash.update (b) f.close () return myhash.hexdigest () def CALCSHA1 (filepath): With Open (fil Epath, ' RB ') as F:sha1obj = HASHLIB.SHA1 () sha1obj.update (F.read ()) hash = Sha1obj.hexdigest () Print (hash) return Hash def CalcMD5 (filepath): With open (filepath, ' RB ') as F:md5obj = Hashlib.md5 () Md5obj.update (F.read ()) hash = Md5obj.hexdigest () print (hash) return hash if __name__ = = "__main__": If Len (sys.argv) ==2:hashfile = sys.argv[1] If not os.path.exists (hashfile): Hash File = Os.path.join (os.pAth.dirname (__file__), hashfile) if not os.path.exists (hashfile): print ("Cannot found file") else CalcMD5 (hashfile) else:calcmd5 (hashfile) #raw_input ("pause") E Lse:print ("No filename")
The MD value of the Python detection file