简体   繁体   English

python 中文件的 Hash sha512 内容

[英]Hash sha512 contents of a file in python

So im writing a smiple File Integrity Monitor.所以我写了一个简单的文件完整性监视器。 And im trying to hash the contents of a file and store it as a baseline.我试图 hash 文件的内容并将其存储为基线。 This is my code:这是我的代码:

from base64 import encode
import hashlib

h =hashlib.sha512()

with open ("baseline.txt", 'r') as f:
    contents = f.read()
    h.update(contents.encode('utf8'))
    hash = h.hexdigest

print(hash)

And this is my output:这是我的 output:

<built-in method hexdigest of _hashlib.HASH object at 0x000001E3096B5250>

Im not sure what is causing the output to come out this way.我不确定是什么导致 output 以这种方式出现。 I suspect that it has to do with how im opening the file and the format it is coming out in. If you could help me with this or point me in the right direction woudld be great.我怀疑这与我打开文件的方式及其输出格式有关。如果你能帮助我或指出我正确的方向会很棒。

hexdigest is a function:) hexdigest是 function :)

Try hash = h.hexdigest() .尝试hash = h.hexdigest()

As Guy wrote, you need to use h.hexdigest() (with parentheses), that's because, in Python, calling functions without parentheses returns link to object in memory .正如Guy所写,您需要使用h.hexdigest() (带括号),这是因为在 Python 中,调用不带括号的函数会返回ZCD69B4957F06CD82917BF3D61980 中 object 的链接 And calling with parentheses you you execute functions code.括号调用你执行函数代码。 Here 's more information about it 这里有更多关于它的信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM