Python获取文件哈希md5、sha256、sha512等方法
Python3计算获取文件、字符MD5、sha256、sha512、sha384、sha224、sha1,不多说,直接上代码。
方便调用
import os
import hashlib# 使用python3.8及以上可以用此方法,写法更简洁。
def file_hash(file_path: str, hash_method) -> str:if not os.path.isfile(file_path):print('文件不存在。')return ''h = hash_method()with open(file_path, 'rb') as f:while b := f.read(8192):h.update(b)return h.hexdigest()# 其它python3版本使用此方法
'''
def file_hash(file_path: str, hash_method) -> str:if not os.path.isfile(file_path):print('文件不存在。')return ''h = hash_method()with open(file_path, 'rb') as f:while True:b = f.read(8192)if not b:break h.update(b)return h.hexdigest()
'''def str_hash(content: str, hash_method, encoding: str = 'UTF-8') -> str:return hash_method(content.encode(encoding)).hexdigest()def file_md5(file_path: str) -> str:return file_hash(file_path, hashlib.md5)def file_sha256(file_path: str) -> str:return file_hash(file_path, hashlib.sha256)def file_sha512(file_path: str) -> str:return file_hash(file_path, hashlib.sha512)def file_sha384(file_path: str) -> str:return file_hash(file_path, hashlib.sha384)def file_sha1(file_path: str) -> str:return file_hash(file_path, hashlib.sha1)def file_sha224(file_path: str) -> str:return file_hash(file_path, hashlib.sha224)def str_md5(content: str, encoding: str = 'UTF-8') -> str:return str_hash(content, hashlib.md5, encoding)def str_sha256(content: str, encoding: str = 'UTF-8') -> str:return str_hash(content, hashlib.sha256, encoding)def str_sha512(content: str, encoding: str = 'UTF-8') -> str:return str_hash(content, hashlib.sha512, encoding)def str_sha384(content: str, encoding: str = 'UTF-8') -> str:return str_hash(content, hashlib.sha384, encoding)def str_sha1(content: str, encoding: str = 'UTF-8') -> str:return str_hash(content, hashlib.sha1, encoding)def str_sha224(content: str, encoding: str = 'UTF-8') -> str:return str_hash(content, hashlib.sha224, encoding)
使用方法:
获取文件md5
file_md5(r'D:\xxx\xxx')
计算字符md5(默认编码为utf-8)
str_md5('666')
使用其它编码计算字符md5
str_md5('666', encoding='gbk')
Python获取文件哈希md5、sha256、sha512等方法相关推荐
- python获取文件路径下的文件_python 获取文件下所有文件或目录os.walk()的实例
在python3.6版本中去掉了os.path.walk()函数 os.walk() 函数声明:walk(top,topdown=True,oneerror=None) 1.参数top表示需要遍历的目 ...
- python获取文件夹里有什么文件+查看特定格式的文件
python获取文件夹里有什么文件+查看特定格式的文件 功能 程序 效果 后续 功能 获取文件夹的的文件+获取文件夹里的特定格式的文件,比如.png等 程序 import os folder = r& ...
- python 获取文件夹所有文件列表_python获取文件夹下所有文件及os模块方法
python获取文件夹下所有文件 方法一:使用os.listdir import os for filename in os.listdir(r'c:\windows'): print filenam ...
- QT学习记录 --- 获取文件哈希值
前言 本来想阐述一番,想了一哈,实在没啥说的,直接上代码了- 获取文件哈希函数 bool Widget::FileHash(QString srcDir) //参数为目标文件路径 {QFile fil ...
- python获取文件路径名_python文件名获取文件路径
如何使用Python获取文件所在目录和文件名 python中如何根据文件名找他的路径.现在我遍历到怎么才能将某一个文件对应的路径找到呢? 遍历用os.walk: import osfrom os.pa ...
- Python 获取文件夹下所有文件
前言 使用Python获取文件夹下的所有文件时,存在多种方式. 1. os.listdir os.listdir:参数为文件夹路径,可以返回文件夹下的所有子文件夹.文件名称. 示例: import o ...
- python 获取文件CRC值
python 获取文件CRC值 crc值在文件改变之前是唯一的 import zlib def crc(fileName):hash = 0for eachLine in open(fileName, ...
- python获取绝对路径_python3中获取文件当前绝对路径的两种方法
方法1: import sys print(sys.argv) 得到文件当前绝对路径字符串的一个列表 ['D:/pycharm/PracticeProject/ClientServerNetworki ...
- python 获取文件后缀名的方法
获取文件的后缀名有好几种方法: 第一种:splittext()方法 os.path.splittext(path)[-1] 第二种:endswith()方法 path = "test_use ...
最新文章
- PLUICameraViewController 拍照页面崩溃
- 几种文件查找命令,whereis ,find ,locate.
- 可以不封神,但是不能不修炼——亚特兰蒂斯之神特斯拉的启示
- IOS中获取各个文件的目录路径的方法和NSFileManager类
- 利用SQL模糊匹配来验证字段是否是日期格式
- delphi64位 char数组转换string中文乱码_使用位运算、值交换等方式反转java字符串-共四种方法...
- Java 之 内部类
- docker 加速镜像 阿里云镜像
- nginx php exec,PHP Web 端如何操作 Nginx 配置
- arcgis 画图问题
- 网页抓取软件-免费网页自动采集抓取任意内容软件
- 中国34个省级行政区2000年-2021年逐月NDVI统计分析结果
- 处理不连续单元格,如sum,sumif,countif
- 迷惘一代、沉默一代、X世代、Z世代……美国的几代人
- maya制作玻璃材质
- 信号强度和dBm的对应关系
- Moon Player正式登陆爱奇艺-奇遇VR应用商店
- STM32加密保护工具 | STM32Trust介绍、解决方案
- 知网CAJ论文 如何转换成pdf
- 【数据库笔记】高性能MySQL:chapter 5 创建高性能的索引
热门文章
- 支付宝Wap支付你了解多少?
- eclipse导入项目乱码
- Linux系统目录介绍
- Wayland入门15:键盘输入
- matlab中证明欧拉公式,欧拉公式证明
- AI:陆奇博士(原微软全球执行副总裁/百度COO/现奇绩创坛创始人兼CEO)演讲之《正视挑战,把握创业创新机会》
- Android之使用URLConnection进行网络编程
- 登录密码规则复杂度:8-20位,必须包含大写字母,小写字母,数字组合,必须包含下面一位特殊字符(#@$%*~)
- vs2019 报错信息:error C2664 解决方法
- 360每日趣玩弹窗怎么彻底删除?