工作中用到了MD5值來進行對文件校驗,MD5本身就是一個很出色的算法,一定程度上解決了hash散列的沖突,關于MD5的內容網上也有很多,這里只要是進行一個實驗,驗證一下文件校驗方面的工作,因為習慣使用了python來完成這樣的任務,這里也是使用python,了解到python本身自帶有hashlib模塊,該模塊中就包含了所需的MD5方法,當然python也有專門的MD5模塊可以使用,使用得當發的大同小異,但是個人覺得hashlib模塊更好用一些,今天就使用python的os、commands還有hashlib三個模塊來進行實驗,其中,前兩個模塊主要是為了可以在python腳本中執行Linux的shell命令,以此來驗證一下同一個文件以及文件內容是否可以得到與hashlib模塊MD5方法相同的MD5值,好了不多說了,下面是程序,很簡單的一個驗證,里面都有詳細的注釋就不多解釋了:
#!/usr/bin/env python# -*- coding: utf-8 -*-'''功能:校驗文件MD5值'''import hashlib, binasciiimport md5import osimport commands#使用python自帶的hashlib庫def get_md5_value(str): my_md5 = hashlib.md5()#獲取一個MD5的加密算法對象 my_md5.update(str) #得到MD5消息摘要 my_md5_Digest = my_md5.hexdigest()#以16進制返回消息摘要,32位 return my_md5_Digest#使用python自帶的hashlib庫如果m.update(a)之后在 m.update(b),那么就相當于m.update(a+b),這里驗證一下def get_md5_value_two(str1, str2): my_md52 = hashlib.md5() my_md52.update(str1) my_md52.update(str2) my_md52_Digest = my_md52.hexdigest() return my_md52_Digest#使用SHA1def get_sha1_value(str): my_sha = hashlib.sha1() my_sha.update(str) my_sha_Digest = my_sha.hexdigest() return my_sha_Digest
#利用os模塊system()方法獲取文件的MD5值 def get_file_md5_value(filename): return os.system('md5sum ' + filename + '|cut -f1') #利用os模塊popen()方法獲取文件的MD5值def get_popen_file_md5_value(filename): return os.popen('md5sum ' + filename + '|cut -f1')#獲取文件內容的MD5值def get_file_content_md5_value(filename): fp = open(filename) fp_content = fp.read() return get_md5_value(fp_content)#利用commands模塊獲取文件的MD5值def get_commands_file_md5_value(filename): return commands.getoutput('md5sum ' + filename + '|cut -f1')
if __name__ == '__main__': string1 = 'We are friends!!!' string2 = 'Do agree with me?' filename = 'Linux.txt' result_md5_value = get_md5_value(string1) result_sha1_value = get_sha1_value(string1) print '-------------------------------------------------' print '原始字符串為:'+string1 print 'hashlib模塊MD5值為:'+result_md5_value,len(result_md5_value) print 'SHA1值為:'+result_sha1_value,len(result_sha1_value) print '-------------------------------------------------' result_md5_value = get_md5_value(string2) result_sha1_value = get_sha1_value(string2) print '原始字符串為:'+string2 print 'hashlib模塊MD5值為:'+result_md5_value,len(result_md5_value) print 'SHA1值為:'+result_sha1_value,len(result_sha1_value) print '----------------------驗證---------------------------' result_md5_value = get_md5_value(string1+string2) result_sha1_value = get_sha1_value(string1+string2) print '原始字符串為:', string1+string2 print 'hashlib模塊MD5值為:'+result_md5_value,len(result_md5_value) print 'SHA1值為:'+result_sha1_value,len(result_sha1_value) print '-----------------------整體MD5--------------------------' result_md52_value = get_md5_value_two(string1, string2) print '原始字符串為:', string1+string2 print 'hashlib模塊MD5值為:'+result_md52_value,len(result_md52_value) print '****************************os模塊獲取文件的MD5值為*******************************************' result_file_value = get_file_md5_value(filename) print result_file_value result_popen_file_md5_value = get_popen_file_md5_value(filename) print result_popen_file_md5_value print '*****************************os模塊獲取文件內容的MD5值為******************************************' result_file_content_value = get_file_content_md5_value(filename) print result_file_content_value, len(result_file_content_value) print '*****************************commands模塊獲取文件的MD5值為******************************************' result_commands_file_md5_value = get_commands_file_md5_value(filename) print result_commands_file_md5_value, len(result_commands_file_md5_value)
新聞熱點
疑難解答