Python實(shí)現(xiàn)計(jì)算文件MD5和SHA1的方法示例
本文實(shí)例講述了Python實(shí)現(xiàn)計(jì)算文件MD5和SHA1的方法。分享給大家供大家參考,具體如下:
不多說(shuō),直接源碼:
#file md5
import sys;
import hashlib;
import os.path;
def GetFileMd5(strFile):
file = None;
bRet = False;
strMd5 = "";
strSha1 = "";
try:
file = open(strFile, "rb");
md5 = hashlib.md5();
sha1 = hashlib.sha1();
strRead = "";
while True:
strRead = file.read(8096);
if not strRead:
break;
else:
md5.update(strRead);
sha1.update(strRead);
#read file finish
bRet = True;
strMd5 = md5.hexdigest();
strSha1 = sha1.hexdigest();
except:
bRet = False;
finally:
if file:
file.close()
return [bRet, strMd5, strSha1];
def writFile(strInfo):
file = None;
file = open("E:\\1.txt", 'w+');
file.write(strInfo);
file.write("\n");
if file:
file.close();
if "__main__" == __name__:
bOK , md5str1, sha1str1 = GetFileMd5("E:\\1.mp3");
print(md5str1);
md5All = md5str1 + "\t" + sha1str1;
md5All += "\n";
bOK , md5str2, sha1str2 = GetFileMd5("E:\\2.mp3");
print(md5str2);
writFile(md5str2 + "\t" +sha1str2);
md5All += (md5str2 + "\t" + sha1str2);
md5All += "\n";
bOK , md5str3, sha1str3 = GetFileMd5("E:\\3.mp3");
print(md5str3);
writFile(md5str3 + "\t" +sha1str3);
md5All += (md5str2 + "\t" + sha1str3);
md5All += "\n";
writFile(md5All);
產(chǎn)生的文件如下:
e712ca35354ff51803b51f3c7db03a81 8417609d07ce1bbd53111f1664ecfb63422749bb
34d7451ef9fbeb4c1ebbf2ed5cb96329 9d7009e1f1cd750f5a795d25491a5d294a45f3b2
34d7451ef9fbeb4c1ebbf2ed5cb96329 8a11f608aee135dd1d4b8c64af0721790e0a0b32
要是自己使用,改吧,改吧就可以使用了。
PS:關(guān)于加密解密感興趣的朋友還可以參考本站在線工具:
文字在線加密解密工具(包含AES、DES、RC4等):
http://tools.jb51.net/password/txt_encode
MD5在線加密工具:
http://tools.jb51.net/password/CreateMD5Password
在線散列/哈希算法加密工具:
http://tools.jb51.net/password/hash_encrypt
在線MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:
http://tools.jb51.net/password/hash_md5_sha
在線sha1/sha224/sha256/sha384/sha512加密工具:
http://tools.jb51.net/password/sha_encode
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python加密解密算法與技巧總結(jié)》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python通過(guò)4種方式實(shí)現(xiàn)進(jìn)程數(shù)據(jù)通信
這篇文章主要介紹了Python通過(guò)4種方式實(shí)現(xiàn)進(jìn)程數(shù)據(jù)通信,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03
Pytorch Conda環(huán)境下載慢換源/刪源/恢復(fù)默認(rèn)源的簡(jiǎn)單操作
隨著實(shí)驗(yàn)增多,需要分割創(chuàng)建環(huán)境的情況時(shí)有出現(xiàn),在此情況下使用conda create --name xx python=3.10 pytorch torchvision pytorch-cuda -c nvidia -c pytorch -y這樣的指令創(chuàng)建時(shí)如果不換源,往往下載速度很慢,本文介紹了解決辦法,需要的朋友可以參考下2024-07-07
bpython 功能強(qiáng)大的Python shell
bpython是一個(gè)不錯(cuò)的Python解釋器的界面,本文帶著大家了解認(rèn)識(shí)一下bpython,感興趣的小伙伴們可以參考一下2016-02-02
Opencv實(shí)現(xiàn)傾斜圖片轉(zhuǎn)正示例
本文主要介紹了Opencv實(shí)現(xiàn)傾斜圖片轉(zhuǎn)正示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
Python?異步如何使用等待有時(shí)間限制協(xié)程
這篇文章主要為大家介紹了Python?異步如何使用等待有時(shí)間限制協(xié)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
python 插入日期數(shù)據(jù)到Oracle實(shí)例
這篇文章主要介紹了python 插入日期數(shù)據(jù)到Oracle實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
詳解Python如何實(shí)現(xiàn)Excel數(shù)據(jù)讀取和寫入
這篇文章主要為大家詳細(xì)介紹了python如何實(shí)現(xiàn)對(duì)EXCEL數(shù)據(jù)進(jìn)行讀取和寫入,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04

