Python Forum

Full Version: python hmac gave different result than php hash_hmac
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
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'
>>>
Thanks! also about the code since it small I didn't format, but I will next time