有关检查文件的问题 [英] Question regarding checksuming of a file
问题描述
晚上好,
我需要生成一个文件的校验和,将值存储在一个变量中,
并将其传递给以后进行比较。
MD5模块似乎可以解决这个问题,但我对实现非常粗略。
我能看到的最接近的是
< br $> b $ b导入md5
m = md5.new()
contents = open(self.file_name," rb")。 ()
check = md5.update(内容)
但是这似乎并没有实际返回校验和。
有没有人能够深入了解我的错误?
我们将非常感谢您提供的任何帮助。
谢谢
Good evening,
I need to generate checksums of a file, store the value in a variable,
and pass it along for later comparison.
The MD5 module would seem to do the trick but I''m sketchy on implementation.
The nearest I can see would be
import md5
m=md5.new()
contents = open(self.file_name,"rb").read()
check=md5.update(contents)
However this does not appear to be actually returning the checksum.
Does anyone have insight into where I am going wrong?
Any help you can provide would be greatly appreciated.
Thanks
推荐答案
Andrew Robert写道:
Andrew Robert wrote:
m = md5.new()
contents = open(self.file_name," rb")。read()
check = md5.update(contents)
然而这似乎并没有真正回归校验和。
m=md5.new()
contents = open(self.file_name,"rb").read()
check=md5.update(contents)
However this does not appear to be actually returning the checksum.
文档是你的朋友,使用它们。提示:首先你吃,然后你...
http ://docs.python.org/lib/module-md5.html
-
Edward Elliott
加州大学伯克利分校法学院(Boalt Hall)
在eddeye dot net抱怨的
the docs are your friend, use them. hint: first you eat, then you...
http://docs.python.org/lib/module-md5.html
--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
实际上,我认为我得到了它但是想要确认这看起来是对的。
import md5
checksum = md5.new()
mfn = open(self.file_name, ''r'')
for mfn.readlines():
checksum.update(line)
mfn.close()
cs = checksum.hexdigest()
print cs
值cs应该包含MD5校验和或者我错过了什么?
我们非常感谢您提供的任何帮助。
谢谢
Actually, I think I got it but would like to confirm this looks right.
import md5
checksum = md5.new()
mfn = open(self.file_name, ''r'')
for line in mfn.readlines():
checksum.update(line)
mfn.close()
cs = checksum.hexdigest()
print cs
The value cs should contain the MD5 checksum or did I miss something?
Any help you can provide would be greatly appreciated.
Thanks
文章< 12 ************* @ corp.supernews.com>,
Andrew Robert< an ********* ***@gmail.com>写道:
In article <12*************@corp.supernews.com>,
Andrew Robert <an************@gmail.com> wrote:
晚上好,
我需要生成文件的校验和,将值存储在变量中,然后传递给它后来的比较。
MD5模块似乎可以解决这个问题,但我对实现很粗略。
我能看到的最接近的是
import md5
m = md5.new()
contents = open(self.file_name," rb")。read()
check = md5.update(内容)
然而,这似乎并没有实际返回校验和。
有没有人能够洞察我出错的地方?
Good evening,
I need to generate checksums of a file, store the value in a variable,
and pass it along for later comparison.
The MD5 module would seem to do the trick but I''m sketchy on implementation.
The nearest I can see would be
import md5
m=md5.new()
contents = open(self.file_name,"rb").read()
check=md5.update(contents)
However this does not appear to be actually returning the checksum.
Does anyone have insight into where I am going wrong?
调用update()后,需要调用digest()。 Update()仅更新
md5状态机的内部状态; digest()返回哈希值。
另外,对于上面的代码,它是m.update(),而不是md5.update()。 Update()是md5实例对象的一个
方法,而不是md5模块本身。
最后,已知md5算法很弱。如果您正在使用md5来保证与某些预先存在的实现的兼容性,那就是一个
的东西。但是,如果你从头开始新的东西,我会建议使用SHA-1代替
(参见sha模块)。 SHA-1比md5更加强大。
加密。 Python API实际上是相同的,所以它没有额外的工作来切换到更强大的算法。
After calling update(), you need to call digest(). Update() only updates
the internal state of the md5 state machine; digest() returns the hash.
Also, for the code above, it''s m.update(), not md5.update(). Update() is a
method of an md5 instance object, not the md5 module itself.
Lastly, the md5 algorithm is known to be weak. If you''re doing md5 to
maintain compatability with some pre-existing implementation, that''s one
thing. But, if you''re starting something new from scratch, I would suggest
using SHA-1 instead (see the sha module). SHA-1 is much stronger
cryptographically than md5. The Python API is virtually identical, so it''s
no added work to switch to the stronger algorithm.
这篇关于有关检查文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!