繁体   English   中英

HMAC python与HMAC php不同

[英]HMAC python differs from HMAC php

我将我的流明代码迁移到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 ,即要hmac.new的消息。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM