如何在Python中计算文件的md5校验和? [英] How do I calculate the md5 checksum of a file in Python?

查看:626
本文介绍了如何在Python中计算文件的md5校验和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中编写了一个代码,用于检查文件中的md5,并确保md5与原始文件中的md5相匹配.这是我开发的:

I have made a code in python that checks for an md5 in a file and makes sure the md5 matches that of the original. Here is what I have developed:

#Defines filename
filename = "file.exe"

#Gets MD5 from file 
def getmd5(filename):
    return m.hexdigest()

md5 = dict()

for fname in filename:
    md5[fname] = getmd5(fname)

#If statement for alerting the user whether the checksum passed or failed

if md5 == '>md5 will go here<': 
    print("MD5 Checksum passed. You may now close this window")
    input ("press enter")
else:
    print("MD5 Checksum failed. Incorrect MD5 in file 'filename'. Please download a    new copy")
    input("press enter") 
exit

但是,每当我运行代码时,我都会得到以下信息:

But whenever I run the code, I get the following:

Traceback (most recent call last):
File "C:\Users\Username\md5check.py", line 13, in <module>
 md5[fname] = getmd5(fname)
File "C:\Users\Username\md5check.py, line 9, in getmd5
  return m.hexdigest()
NameError: global name 'm' is not defined

我的代码中缺少什么吗?

Is there anything I am missing in my code?

推荐答案

关于您的错误以及代码中缺少的内容. m是未为getmd5()功能定义的名称.不冒犯,我知道您是一个初学者,但是您的代码无处不在.让我们一个个地看一下您的问题:)首先,您没有正确使用hashlib.md5.hexdigest()方法.请在 Python文档库中找到有关hashlib函数的说明.为提供的 string 返回MD5的正确方法是执行以下操作:

In regards to your error and what's missing in your code. m is a name which is not defined for getmd5() function. No offence, I know you are a beginner, but your code is all over the place. Let's look at your issues one by one :) First off, you are not using hashlib.md5.hexdigest() method correctly. Please find explanation on hashlib functions Python Doc Library. The correct way to return MD5 for provided string is to do something like this:

>>> import hashlib
>>> hashlib.md5("filename.exe").hexdigest()
'2a53375ff139d9837e93a38a279d63e5'

但是,您在这里遇到了更大的问题.您正在根据文件名字符串计算MD5,实际上MD5是根据文件内容计算的.您将需要基本读取文件内容并将其通过MD5传输.我的下一个示例效率不是很高,但是类似这样:

However, you have a bigger problem here. You are calculating MD5 on a file name string, where in reality MD5 is calculated based on file contents. You will need to basically read file contents and pipe it though MD5. My next example is not very efficient, but something like this:

>>> import hashlib
>>> hashlib.md5(open('filename.exe','rb').read()).hexdigest()
'd41d8cd98f00b204e9800998ecf8427e'

您可以清楚地看到第二个MD5哈希与第一个完全不同.这样做的原因是我们要推送文件的内容,而不仅仅是文件名.一个简单的解决方案可能是这样的:

As you can clearly see second MD5 hash is totally different from the first one. The reason for that is that we are pushing contents of the file through, not just file name. A simple solution could be something like that:

# Import hashlib library (md5 method is part of it)
import hashlib

# File to check
file_name = 'filename.exe'

# Correct original md5 goes here
original_md5 = '5d41402abc4b2a76b9719d911017c592'  

# Open,close, read file and calculate MD5 on its contents 
with open(file_name) as file_to_check:
    # read contents of the file
    data = file_to_check.read()    
    # pipe contents of the file through
    md5_returned = hashlib.md5(data).hexdigest()

# Finally compare original MD5 with freshly calculated
if original_md5 == md5_returned:
    print "MD5 verified."
else:
    print "MD5 verification failed!."

请查看帖子 Python:生成MD5文件的校验和 它详细说明了如何有效实现文件的几种方法.

Please look at the post Python: Generating a MD5 checksum of a file it explains in detail a couple of ways how it can be achieved efficiently.

祝你好运.

这篇关于如何在Python中计算文件的md5校验和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆