用python计算md5,sha1,crc32
2024-08-26 08:31:16
Linux下计算md5sum,sha1sum,crc:
命令 输出
$md5sum hello f19dd746bc6ab0f0155808c388be8ff0 hello
$sha1sum hello 79e560a607e3e6e9be2c09a06b7d5062cb5ed566 hello
$crc32 hello 327213a2
Python也能做这个工作,其中md5和sha1需import hashlib
, crc32可以import zlib
#test.py
#!/usr/bin/env python from hashlib import md5, sha1
from zlib import crc32
import sys def getMd5(filename): #计算md5
mdfive = md5()
with open(filename, 'rb') as f:
mdfive.update(f.read())
return mdfive.hexdigest() def getSha1(filename): #计算sha1
sha1Obj = sha1()
with open(filename, 'rb') as f:
sha1Obj.update(f.read())
return sha1Obj.hexdigest() def getCrc32(filename): #计算crc32
with open(filename, 'rb') as f:
return crc32(f.read()) if len(sys.argv) < 2:
print('You must enter the file')
exit(1)
elif len(sys.argv) > 2:
print('Only one file is permitted')
exit(1) filename = sys.argv[1] print('{:8} {}'.format('md5:', getMd5(filename)))
print('{:8} {}'.format('sha1:', getSha1(filename)))
print('{:8} {:x}'.format('crc32:', getCrc32(filename)))
$python test.py hello
结果:
md5: f19dd746bc6ab0f0155808c388be8ff0
sha1: 79e560a607e3e6e9be2c09a06b7d5062cb5ed566
crc32: 327213a2
最新文章
- 优化SQLServer--表和索引的分区(二)
- ASP.NET Web API 创建帮助页
- 【leetcode】Convert Sorted Array to Binary Search Tree
- PHP---------PHP函数里面的static静态变量
- Authentication和Authorization的区别
- 线程本地存储TLS(Thread Local Storage)的原理和实现&mdash;&mdash;分类和原理
- 如何设置DB2I(SPUFI)来正常工作
- 类名.class与类名.this详解
- Selenium 下载URL
- 纠错记录(Could not open the editor: Android XML Editor cannot process this input.)
- sn9c291 驱动载入成功,mpayer无法播放
- [翻译]高并发框架 LMAX Disruptor 介绍
- python学习:绝对路径和相对路径
- Flip Game---poj1753(状压+bfs)
- splice的多种用法
- 解决thinkphp设置session周期无效的问题
- 【websocket-sharp】使用
- asp.net部署时加密config文件
- Hibernate的调用数据库的存储过程
- LeetCode OJ:Rotate List(旋转链表)