Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking SHA1 on a String

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?

like image 821
hax0r_n_code Avatar asked Dec 29 '15 15:12

hax0r_n_code


People also ask

Is SHA-1 a string?

The sha1 algorithm generates a 160-bit binary string result.

What is SHA-1 command?

What is the sha1sum command in UNIX? The sha1sum command computes the SHA-1 message digest of a file. This allows it be compared to a published message digest to check whether the file is unmodified from the original. As such the sha1sum command can be used to attempt to verify the integrity of a file.

How do I know if SHA-1 is on my Mac?

Method 2: Using OpenSSL to verify SHA-1 Or you can type the command openssl sha1 followed by space and drag and drop the file to the Terminal. Wait a while and you should see SHA1(/the/full/path/to/your/file)= followed by the checksum.


1 Answers

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.

like image 89
Martijn Pieters Avatar answered Oct 14 '22 12:10

Martijn Pieters