0

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?

ensnare
  • 40,069
  • 64
  • 158
  • 224

1 Answers1

1

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()
Sraw
  • 18,892
  • 11
  • 54
  • 87
  • how would i calculate this from the binary output of Popen? Is it not better to calculate everything in the shell and return only the sha512 hash? – ensnare May 22 '18 at 13:52
  • You can just pass the stream inside `stdout` to `sha512`. `hashlib.sha512(result.communicate()[0])`. The reason why I don't think create another subprocess is a good idea is it will cost you a little bit more as you are creating another process. If you can do it in one process, why not? – Sraw May 23 '18 at 02:09