简体   繁体   中英

how to reproduce the same python hmac with base64 output in matlab

I have this python code snippet that create a signature

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

the ouput of the signature is 7WBwddbqe2BQEvLC20bwOFPt2fk=

by running the debugger, i can see the message_to_sign and my private_key

在此处输入图片说明

My understanding is i should get the same result if i run the hmac function and then base64 encoding function but it shows different result as an example below:

if i set in 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=

the hmac function that i tried:

  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=

the base64:

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

python version: 2.7.13

matlab version: 2016a

How can i reproduce the same result from python?

我已经通过使用适当的换行符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'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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