Python Learning (30)----Python to implement file MD5 checksum __python

Source: Internet
Author: User
Tags md5 md5 encryption

Linux to verify the file MD5 value, the easiest way is to execute the md5sum command
md5sum filename
Originally intended to use subprocess to invoke system commands to get MD5 values,

Import Subprocess,shlex
cmd = "md5sum filename"
p = subprocess (Shlex.split (cmd), stdout=subprocess. PIPE)
print p.stdout.read ()

But Python has its own MD5 module Hashlib, which is much simpler to use,
Instructions for using the Python hashlib module http://docs.python.org/2/library/hashlib.html
FD = HASHLIB.MD5 () #获取一个MD5加密算法对象
Fd.update ("string") #指定需要加密的字符串
Fd.hexdigest () #获取加密后的16进制字符串

Instance

#!/usr/bin/env python           
 #coding: Utf-8 3  4 import sys 
 import hashlib                  
                                 
 def md5sum (filename):           
     fd = open ( FileName, "R")
     Fcont = FD.R
     fd.close ()         
     fmd5 = HASHLIB.MD5 (fcont) return
     fmd5             
                                 
 If __name__ = "__ main__ ":      
     fmd5 = md5sum (sys.argv[1])
     print fmd5.hexdigest ()  

where fmd5 = Hashlib.md5 (Fcont) is equivalent to
FMD5 = HASHLIB.MD5 (Fcont)
Fmd5.update (Fcont)

It should be noted that the incoming HASHLIB.MD5 () should be the contents of the file rather than the filename, so that the contents of the file generated MD5 check code;
In addition, the call to Hashlib.md5 () after the return of an object, want to get the same effect under Linux md5sum, but also call the Hexdigest () method.

However, this method is a bit too rough, when the test of large files, all the contents of the file read into memory, it is very expensive,
The example http://blog.csdn.net/shanliangliuxing/article/details/10115397 is given on the internet,
According to the file block length, get the contents of the file sequentially read into memory, update the checksum through update (),

#!/usr/bin/env python 2 #coding: Utf-8 3 import hashlib def md5hex (word): "" "MD5 encryption algorithm, return 32 bit lowercase 16 symbol" " If Isinstance (Word, Unicode): Word = Word.encode ("Utf-8") Elif not isinstance (Word, str): WOR d = str (word) m = hashlib.md5 () m.update (word) return m.hexdigest () def md5sum (fname): "" "The MD of the computed file
             5 Value "" "Def Read_chunks (FH): Fh.seek (0) chunk = fh.read (8096) while chunk: Yield chunk 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.__clas s__.__name__ in ["Stringio", "StringO"]/or Isinstance (fname, file): for chunk in Read_chunks (fname ): M.update(chunk) Else:return "" Return M.hexdigest () 

 

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.