Reputation: 6391
The word Fox
produces the following sha1 hash:
dfcd3454bbea788a751a696c24d97009ca992d17
In python I'm simply trying to get this same output by doing the following:
import hashlib
myhash = hashlib.sha1("Fox".encode('utf-8'))
myhash
just produces the following byte object:
b'\xdf\xcd4T\xbb\xeax\x8au\x1ail$\xd9p\t\xca\x99-\x17'
I've tried binascii
and none of the methods there seem to be able to produce the above output.
How can I produce the resulting ascii hash from here?
Upvotes: 6
Views: 8039
Reputation: 1121854
You have a hexadecimal representation of a digest. You can use the hash.hexdigest()
method to produce the same in Python:
>>> import hashlib
>>> myhash = hashlib.sha1("Fox".encode('utf-8'))
>>> myhash.digest()
b'\xdf\xcd4T\xbb\xeax\x8au\x1ail$\xd9p\t\xca\x99-\x17'
>>> myhash.hexdigest()
'dfcd3454bbea788a751a696c24d97009ca992d17'
You could also convert the binary digest to hexadecimal with the binascii.hexlify()
function:
>>> import binascii
>>> binascii.hexlify(myhash.digest())
b'dfcd3454bbea788a751a696c24d97009ca992d17'
>>> binascii.hexlify(myhash.digest()).decode('ascii')
'dfcd3454bbea788a751a696c24d97009ca992d17'
However, that's just a more verbose way of achieving the same thing.
Upvotes: 16