Reputation: 42013
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?
Upvotes: 0
Views: 601
Reputation: 20196
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()
Upvotes: 1