简体   繁体   中英

In Python 3, calculate a checksum on binary output from command line executable

I'm using a tool called "exiftool" to extract a binary JPG from a file. I would like to then compute a sha512 sum from that file. What is a good way to do this?

My function to extract the binary JPG is as follows:

def getVisSig(filename):
""" Calculates visual signature using
    imagemagick package. returns signature
"""

print("Calculating VisSig on %s" % filename)
result = subprocess.Popen(["exiftool","-b","-PreviewImage",
                           filename,], stdout=subprocess.PIPE)

The output is binary. How should I handle this to compute a sha512 sum? I was thinking I could pipe the output to sha512sum in the command line and read the resulting string into Python, but not sure if there is a better way?

Take a look at https://docs.python.org/3/library/hashlib.html

For example:

import hashlib

hashlib.sha512(b"asdfasdf").hexdigest()
# output: 'ce57d8bc990447c7ec35557040756db2a9ff7cdab53911f3c7995bc6bf3572cda8c94fa53789e523a680de9921c067f6717e79426df467185fc7a6dbec4b2d57'

So you can just:

hashlib.sha512(result).hexdigest()

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