Python Forum
python hmac gave different result than php hash_hmac
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python hmac gave different result than php hash_hmac
#1
on python:
import hashlib
import hmac

hmac.new(b'abc', b'skey', hashlib.sha256).hexdigest()

return: '24fd94ffa1f3e4c62376205b715a9193b67b568731b1c1f661fd1c1bc717e469'
but in php:
Output:
hash_hmac('sha256', 'abc', 'skey') return: 'ee6177854ca54cfcc49f1bf44256eefc0f9f3fb197c9af9ddf821fa4282b5906'
Why it is different?

I try convert code from php to python.

Test on python versions: 3.6.6, 3.5.4 and php: 7.0.30
Reply
#2
you have switched the place of key and msg positional arguments for python example.

>>> import hashlib
>>> import hmac
>>> hmac.new(b'skey', b'abc', hashlib.sha256).hexdigest()
'ee6177854ca54cfcc49f1bf44256eefc0f9f3fb197c9af9ddf821fa4282b5906'
>>> hmac.new(key=b'skey', msg=b'abc', digestmod=hashlib.sha256).hexdigest()
'ee6177854ca54cfcc49f1bf44256eefc0f9f3fb197c9af9ddf821fa4282b5906'
>>>
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thanks! also about the code since it small I didn't format, but I will next time
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Palindrome in Python - result always false RavCOder 13 6,703 Oct-16-2019, 01:38 PM
Last Post: perfringo
  Post JSON from python to PHP don't give expected result pgagnebin 1 3,736 Sep-04-2019, 10:29 PM
Last Post: micseydel
  python result problem of an iteration algorithm for power allocation Jessica 1 2,622 Sep-07-2018, 08:08 PM
Last Post: micseydel
  python code (embedded in Redshift) to return result of the query Mel 0 2,449 Aug-24-2018, 06:12 PM
Last Post: Mel

Forum Jump:

User Panel Messages

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