简体   繁体   中英

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. 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:

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

Im not sure what is causing the output to come out this way. 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:)

Try 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 . And calling with parentheses you you execute functions code. Here 's more information about it

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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