Python实现计算文件MD5和SHA1的方法示例
作者:Qyee16 时间:2023-12-07 06:55:46
本文实例讲述了Python实现计算文件MD5和SHA1的方法。分享给大家供大家参考,具体如下:
不多说,直接源码:
#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);
产生的文件如下:
e712ca35354ff51803b51f3c7db03a81 8417609d07ce1bbd53111f1664ecfb63422749bb
34d7451ef9fbeb4c1ebbf2ed5cb96329 9d7009e1f1cd750f5a795d25491a5d294a45f3b2
34d7451ef9fbeb4c1ebbf2ed5cb96329 8a11f608aee135dd1d4b8c64af0721790e0a0b32
要是自己使用,改吧,改吧就可以使用了。
PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:
文字在线加密解密工具(包含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
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/qyee16/article/details/50086603
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mysql 8.0.14 安装配置方法图文教程
![](https://img.aspxhome.com/file/2023/9/115339_0s.png)
Python中SQLite数据库的使用
Python多线程以及多线程中join()的使用方法示例
![](https://img.aspxhome.com/file/2023/0/90600_0s.png)
神经网络理论基础及Python实现详解
![](https://img.aspxhome.com/file/2023/1/90791_0s.png)
pytorch超详细安装教程之Anaconda、PyTorch和PyCharm全套安装流程
![](https://img.aspxhome.com/file/2023/5/97375_0s.jpg)
switchery按钮的使用方法
![](https://img.aspxhome.com/file/2023/3/132593_0s.png)
django 自定义filter 判断if var in list的例子
vue基础语法之插值表达式详解
![](https://img.aspxhome.com/file/2023/8/126568_0s.jpg)
用python实现五子棋实例
![](https://img.aspxhome.com/file/2023/8/65908_0s.jpg)
Python数据结构之链表详解
![](https://img.aspxhome.com/file/2023/6/80796_0s.png)
解读ASP.NET 5 & MVC6系列教程(2):初识项目
![](https://img.aspxhome.com/file/2023/8/75528_0s.png)
跟我学习javascript的异步脚本加载
golang中的空slice案例
![](https://img.aspxhome.com/file/2023/3/101043_0s.png)
pytorch载入预训练模型后,实现训练指定层
python获取外网ip地址的方法总结
opencv-python 提取sift特征并匹配的实例
Python实现免费音乐下载器
![](https://img.aspxhome.com/file/2023/3/97213_0s.gif)
ASP+FSO+框架实现ASP生成htm并分页的方法(批量)
Laravel 5使用Laravel Excel实现Excel/CSV文件导入导出的功能详解
![](https://img.aspxhome.com/file/2023/3/129843_0s.png)