Using Python's Hashlib module to extract MD5, online reference, think this is good, can be used as a module directly.
#-*-coding:utf-8-*-import hashlib import sys import osdef md5hex (word): "" MD5 encryption algorithm, return 32-bit lowercase 16 binary symbol "" " If Isinstance (Word, Unicode): Word = Word.encode ("Utf-8") Elif not isinstance (Word, str): Word = St R (Word) m = hashlib.md5 () m.update (word) return m.hexdigest () def md5sum (fname): "" Calculates the MD5 value of the file "" " def read_chunks (FH): Fh.seek (0) chunk = fh.read (8096) while Chunk:yield Chu NK chunk = Fh.read (8096) Else: #最后要将游标放回文件开头 fh.seek (0) m = hashlib.md5 () if Isinstance (fname, basestring) and Os.path.exists (fname): With open (fname, "RB") as Fh:for Chunk in Read_chunks (FH): M.update (chunk) #上传的文件缓存 or open file stream elif fname.__class__.__name__ in ["Stringio "," StringO "] or isinstance (fname, file): for chunk in Read_chunks (fname): m.update (Chunk) Else : Return "" Return M.hexdigest () if __name__ = = "__main__": Print (Md5hex (sys.argv[1])) print (Md5sum (sys.argv[2)))
Authentication on Linux:
Python extract MD5