- Machine Learning for Cybersecurity Cookbook
- Emmanuel Tsukerman
- 118字
- 2021-06-24 12:29:02
How to do it...
In the following steps, we will see how to obtain the hash of a file:
- Begin by importing the libraries and selecting the desired file you wish to hash:
import sys
import hashlib
filename = "python-3.7.2-amd64.exe"
- Instantiate the MD5 and SHA256 objects, and specify the size of the chunks we will be reading:
BUF_SIZE = 65536
md5 = hashlib.md5()
sha256 = hashlib.sha256()
- We then read in the file in chunks of 64 KB and incrementally construct our hashes:
with open(filename, "rb") as f:
while True:
data = f.read(BUF_SIZE)
if not data:
break
md5.update(data)
sha256.update(data)
- Finally, print out the resulting hashes:
print("MD5: {0}".format(md5.hexdigest()))
print("SHA256: {0}".format(sha256.hexdigest()))
This results in the following output:
MD5: ff258093f0b3953c886192dec9f52763
SHA256: 0fe2a696f5a3e481fed795ef6896ed99157bcef273ef3c4a96f2905cbdb3aa13
推薦閱讀
- Hands-On Deep Learning with Apache Spark
- Getting Started with Containerization
- PHP開發手冊
- OpenStack Cloud Computing Cookbook(Second Edition)
- 大數據技術與應用
- 工業機器人運動仿真編程實踐:基于Android和OpenGL
- 面向對象程序設計綜合實踐
- Dreamweaver CS6中文版多功能教材
- 計算機組成與操作系統
- 21天學通Linux嵌入式開發
- 機床電氣控制與PLC
- 機器學習案例分析(基于Python語言)
- Hadoop Beginner's Guide
- 分布式Java應用
- 智能小車機器人制作大全(第2版)