如何在matlab中使用base64输出重现相同的python hmac

问题描述 投票:0回答:1

我有这个创建签名的python代码片段

signature = base64.b64encode(hmac.new(self.__private_key, message_to_sign, sha).digest())

签名的输出是7WBwddbqe2BQEvLC20bwOFPt2fk =

通过运行调试器,我可以看到message_to_sign和我的private_key

enter image description here

我的理解是,如果我运行hmac函数然后运行base64编码函数,我应该得到相同的结果,但它显示不同的结果,如下例所示:

如果我在matlab中设置

msg= 'GET\n\n\nFri, 02 Mar 2018 16:31:09 +0000\n/api/annotation/5357434.json';
hash = HMAC(key,message,'SHA-1');
test = base64encode(hash);

test = / svUNw1mx9nMndf7aXvQEUu + NkQ =

我试过的hmac函数:

  1. https://www.mathworks.com/matlabcentral/fileexchange/46182-hmac-hash-message-authentication-code-function
  2. https://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/34162/versions/1/previews/doHMAC_SHA1.m/index.html?access_key=

base64:

  1. https://www.mathworks.com/matlabcentral/fileexchange/39526-byte-encoding-utilities

python版本:2.7.13

matlab版:2016a

我怎样才能从python中重现相同的结果?

python matlab base64 hmacsha1
1个回答
0
投票

我通过使用正确的换行符来解决这个问题,这个换行符是char(10)而不是之前的/ n。

message=['GET',char(10),char(10),char(10),'Fri, 02 Mar 2018 16:31:09 +0000',char(10),'/api/annotation/5357434.json'];
© www.soinside.com 2019 - 2024. All rights reserved.