Python extract MD5

Source: Internet
Author: User
Tags md5 encryption

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.