Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
hash v2 and v3 help
#1
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.
Reply
#2
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'
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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())
Reply
#4
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#5
You're right.

I think the problem is that he's converting to the bytes the "key" string instead of "c41d9eac" :)
Reply
#6
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.
Reply
#7
(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.
Reply
#8
Oh my gosh, b'key'. I did not see it.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] How to crack hash with hashlib Milan 0 1,414 Mar-09-2023, 08:25 PM
Last Post: Milan
  Hash command works differently for me in CMD and Spyder ZweiDCG 3 2,351 Sep-10-2019, 01:10 PM
Last Post: DeaD_EyE
  length constraint on phrase hash to password javaben 0 1,912 Aug-21-2019, 05:34 PM
Last Post: javaben
  Create file archive that contains crypto hash ED209 1 2,057 May-29-2019, 03:05 AM
Last Post: heiner55
  Fastest dict/map method when 'key' is already a hash? tasket 6 3,993 Apr-20-2019, 06:40 PM
Last Post: tasket
  how to generate sha256 hash for each line of my txt file | using python version 3.6.4 rajtekken5 2 9,093 Feb-11-2018, 01:41 PM
Last Post: rajtekken5
  virtualenv activate.ps1 hash error po20 2 3,822 Jan-13-2018, 09:21 AM
Last Post: po20
  Hash function in Python rachelrosemond 3 4,036 Sep-29-2017, 02:57 PM
Last Post: ichabod801
  Python Hash list check here2learn 4 5,403 Feb-27-2017, 08:35 PM
Last Post: here2learn
  fast hash function verstapp 3 6,038 Dec-13-2016, 07:10 PM
Last Post: verstapp

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020