2017-06-23 54 views
1

我想產生一個PHP HMAC_SHA1差異,且openresty盧阿驗證它HMAC_SHA1對PHP和Lua

PHP代碼:

$hmac_sha1 = hash_hmac('sha1', 'test', 'gabri', true); 
echo base64_encode($hmac_sha1); 

主要生產:

/ReAJgDe67/lF3BNbaGSCx70J/c= 

和lua中的相同代碼:

local hmac_sha1 = ngx.hmac_sha1("test", "gabri") 
ngx.log(ngx.NOTICE, ngx.encode_base64(hmac_sha1)); 

產生:

Yczcenrc2EAOpfm9UEWwME9XLRI= 

它們爲什麼不同?

在PHP中,我將在hash_hmac的第四個參數,它返回的數據作爲原始二進制

按: https://github.com/openresty/lua-nginx-module#ngxhmac_sha1

的HMAC-SHA1摘要的原始二進制形式將例如,使用 ngx.encode_base64來將結果編碼爲文本 表示,如果需要的話。

+0

可疑的這個函數返回值作爲二進制字符串和一個十六進制字符串。 – moteus

+0

看起來像PHP是錯誤的,按照nginx的文檔, _local鍵= 「thisisverysecretstuff」 _ _local SRC = 「一些字符串,我們要簽署」 _ _local摘要= ngx.hmac_sha1(鍵,SRC)_ _ngx。說(ngx.encode_base64(摘要))_ 產生輸出 _R/pvxzHC4NLtj7S + kXFg/NePTmk = _ - 但在PHP中: _ $關鍵= 「thisisverysecretstuff」; _ _ $ SRC =「一些我們想要簽名的字符串「; _ _ $ digest = hash_hmac('sha1',$ key,$ src,true); _ _echo base64_encode($ digest); _ 收益率 _y2h0PgNY9Xfil5xe6wCXLhTcPZI = _ –

+0

$ digest = hash_hmac('sha1',$ src,$ key,true); – moteus

回答

0

根據文檔

string hash_hmac(string $algo, string $data, string $key [, bool $raw_output = false]) digest = ngx.hmac_sha1(secret_key, str)

所以hash_hmac('sha1', 'test', 'gabri', true);test是數據和gabri是關鍵。 但ngx.hmac_sha1("test", "gabri")gabri是數據,而test是關鍵