T
T
tincap2016-07-22 13:08:58
Python
tincap, 2016-07-22 13:08:58

How to generate a hash code based on a key in Python3?

In PHP, one function is enough to generate a hash code based on a key: To try to figure this out in Python3, I had to pull out a lot of hair from my head. Either use hashlib.sha256() or hmac.new - I still don't understand. Question in title
echo hash_hmac("sha256", "text", "signature");

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2016-07-22
@tincap

>>> import hmac, hashlib
>>> res = hmac.new('signature', 'text', hashlib.sha256)
>>> res.hexdigest()
'3bd9ef87a8de4633a588904d24a64670905a3375687ae52e3daeba82dd702269'

In the 3rd python, you need to additionally convert the strings:
>>> hmac.new(bytearray('signature','utf-8'), bytearray('text','utf-8'), hashlib.sha256).hexdigest()
'3bd9ef87a8de4633a588904d24a64670905a3375687ae52e3daeba82dd702269'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question