HMAC python 不同于 HMAC php
HMAC python differs from HMAC php
我正在将我的 lumen 代码迁移到 python,对于 hmac 函数,我有这个:
PHP
$hash = hash_hmac(
'sha256',
'user@email.com',
'message'
);
Python 3
import hmac
import hashlib
user_hash = hmac.new(b'user@email.com', b'message', hashlib.sha256).hexdigest()
问题是两个结果不匹配:
PHP输出
413777aac2561ca3acd6d49c95df9ecae4c6e2f6bc9adc40bbb77650d7b4c459
Python输出
42879f50e909799d93b835a81a65c03cf78a56ef1c038ac75c8ab3f211d083ea
我想问题是 python 3 如何解释字符串,但我无法弄明白。有什么帮助吗?
HMAC 的参数顺序有所不同:
>>> hmac.new(b'user@email.com', b'message', hashlib.sha256).hexdigest()
'42879f50e909799d93b835a81a65c03cf78a56ef1c038ac75c8ab3f211d083ea'
>>> hmac.new(b'message', b'user@email.com', hashlib.sha256).hexdigest()
'413777aac2561ca3acd6d49c95df9ecae4c6e2f6bc9adc40bbb77650d7b4c459'
在hmac.new
中,第一个参数是key
(散列的起始键),第二个参数是msg
,要消化的消息。
我正在将我的 lumen 代码迁移到 python,对于 hmac 函数,我有这个:
PHP
$hash = hash_hmac(
'sha256',
'user@email.com',
'message'
);
Python 3
import hmac
import hashlib
user_hash = hmac.new(b'user@email.com', b'message', hashlib.sha256).hexdigest()
问题是两个结果不匹配:
PHP输出
413777aac2561ca3acd6d49c95df9ecae4c6e2f6bc9adc40bbb77650d7b4c459
Python输出
42879f50e909799d93b835a81a65c03cf78a56ef1c038ac75c8ab3f211d083ea
我想问题是 python 3 如何解释字符串,但我无法弄明白。有什么帮助吗?
HMAC 的参数顺序有所不同:
>>> hmac.new(b'user@email.com', b'message', hashlib.sha256).hexdigest()
'42879f50e909799d93b835a81a65c03cf78a56ef1c038ac75c8ab3f211d083ea'
>>> hmac.new(b'message', b'user@email.com', hashlib.sha256).hexdigest()
'413777aac2561ca3acd6d49c95df9ecae4c6e2f6bc9adc40bbb77650d7b4c459'
在hmac.new
中,第一个参数是key
(散列的起始键),第二个参数是msg
,要消化的消息。