国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python 獲取字符串MD5值方法

2020-02-15 21:34:04
字體:
來源:轉載
供稿:網(wǎng)友

工作中用到了MD5值來進行對文件校驗,MD5本身就是一個很出色的算法,一定程度上解決了hash散列的沖突,關于MD5的內(nèi)容網(wǎng)上也有很多,這里只要是進行一個實驗,驗證一下文件校驗方面的工作,因為習慣使用了python來完成這樣的任務,這里也是使用python,了解到python本身自帶有hashlib模塊,該模塊中就包含了所需的MD5方法,當然python也有專門的MD5模塊可以使用,使用得當發(fā)的大同小異,但是個人覺得hashlib模塊更好用一些,今天就使用python的os、commands還有hashlib三個模塊來進行實驗,其中,前兩個模塊主要是為了可以在python腳本中執(zhí)行Linux的shell命令,以此來驗證一下同一個文件以及文件內(nèi)容是否可以得到與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')#獲取文件內(nèi)容的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模塊獲取文件內(nèi)容的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)            
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 凤冈县| 吉首市| 赞皇县| 堆龙德庆县| 忻州市| 洛南县| 买车| 三河市| 石河子市| 涪陵区| 临汾市| 靖宇县| 莱芜市| 星子县| 晋江市| 密山市| 奉新县| 潮安县| 卫辉市| 昂仁县| 巴彦淖尔市| 洛南县| 闵行区| 调兵山市| 中西区| 永和县| 白山市| 镇江市| 洪江市| 新竹县| 河津市| 普陀区| 祁连县| 波密县| 比如县| 东明县| 巩留县| 双鸭山市| 隆子县| 米脂县| 凯里市|