Python Forum
hash v2 and v3 help - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: hash v2 and v3 help (/thread-9105.html)



hash v2 and v3 help - Normalitie - Mar-21-2018

Hi there - first post from a Python noob.

I'm trying to understand why some code in Python 2.7 works, yet when I try to port it across to Python 3.5 does not.

In Python 2.7
import hashlib
key = 'c41d9eac'
print(hmac.new(key, 'this is the life', hashlib.sha1).hexdigest())
>>> 537e629995a1ddd6c701c8b3563d06971a5cf751

In Python 3.5
import hashlib
key = 'c41d9eac'
print(hmac.new(b'key', b'this is the life', hashlib.sha1).hexdigest())
>>> e4b48f8bc8aa40fbc59fd88deb76bf4fc5e9b645

Any help would be much appreciated. I am really stumped on this.

R.


RE: hash v2 and v3 help - DeaD_EyE - Mar-21-2018

Hm, very strange. I get this result:

andre@andre-GP70-2PE:~$ ipython2
Python 2.7.14 (default, Sep 23 2017, 22:06:14) 
Type "copyright", "credits" or "license" for more information.

IPython 5.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import hashlib, hmac

In [2]: hmac.new(b'key', b'this is the life', hashlib.sha1).hexdigest()
Out[2]: 'e4b48f8bc8aa40fbc59fd88deb76bf4fc5e9b645'

In [3]: hmac.new('key', 'this is the life', hashlib.sha1).hexdigest()
Out[3]: 'e4b48f8bc8aa40fbc59fd88deb76bf4fc5e9b645'



RE: hash v2 and v3 help - ODIS - Mar-21-2018

Try to convert string to the bytes with the encode() method in Python 3.5:

import hashlib
import hmac

key = 'c41d9eac'
print(hmac.new(key.encode(), 'this is the life'.encode(), hashlib.sha1).hexdigest())



RE: hash v2 and v3 help - DeaD_EyE - Mar-21-2018

This is not the problem. He has used ASCII letters inside the bytestring.
The Problem seems to be on Python 2.x side.

The result from his Python 3 example is right.


RE: hash v2 and v3 help - ODIS - Mar-21-2018

You're right.

I think the problem is that he's converting to the bytes the "key" string instead of "c41d9eac" :)


RE: hash v2 and v3 help - Normalitie - Mar-22-2018

Thanks for all your replies.

It's interesting your comment that "v3 is right". This is a snippet of code from a larger script, which sends a url request to a website, requiring SHA1 encryption. The v2 code works, whereas the v3 does not.

I'll try the .encode() suffix to the variables and report back.

R.


RE: hash v2 and v3 help - Normalitie - Mar-22-2018

(Mar-21-2018, 01:41 PM)ODIS Wrote: You're right.

I think the problem is that he's converting to the bytes the "key" string instead of "c41d9eac" :)

Thanks. This was indeed the problem.
Original:
import hashlib
key = 'c41d9eac'
print(hmac.new(b'key', b'this is the life', hashlib.sha1).hexdigest())
Solution:
import hashlib
key = b'c41d9eac'
raw = 'this is the life'
print(hmac.new(key, raw.encode(), hashlib.sha1).hexdigest())
Thanks again for your help.

R.


RE: hash v2 and v3 help - DeaD_EyE - Mar-22-2018

Oh my gosh, b'key'. I did not see it.