2017-03-03 19 views
0

我試圖使用函數來生成base64中的hmac散列。但是,端點無法驗證該消息。API在函數中生成時無法驗證hmac + base64

當我在函數外生成base64中的hmac散列時,它工作正常。端點驗證消息。

這是一個用函數的時候我在做什麼(這是我需要的,不能使其工作):

function genNonce() { 
    list($usec, $sec) = explode(" ", microtime()); 
    $nonce = (int)((float)$usec + (float)$sec); 
    return $nonce; 
} 

function genMsg($nonce) { 
    $signature = strtoupper(base64_encode(hash_hmac('sha256', $nonce . $key, $secret, true))); 
    return $signature; 
} 

$nonce = genNonce(); 
$signature = genMsg($nonce); 

$auth = Array(); 
    $auth[] = "key: " . $key; 
    $auth[] = "nonce: " . $nonce; 
    $auth[] = "signature: " . $signature; 

...execute curl... 

我執行的功能,權當試圖調試傾銷瓦爾在執行卷曲之前,它們匹配。所以它似乎正在生成信息並將其發送出去。

這是不使用的功能(這是它是如何工作的),當我在做什麼:

list($usec, $sec) = explode(" ", microtime()); 
$nonce = (int)((float)$usec + (float)$sec); 
$signature = strtoupper(base64_encode(hash_hmac('sha256', $nonce . $key, $secret, true))); 

$auth = Array(); 
    $auth[] = "key: " . $key; 
    $auth[] = "nonce: " . $nonce; 
    $auth[] = "signature: " . $signature; 

...execute curl... 

難道我做錯了什麼?爲什麼我會得到不同的結果?這是端點API的問題嗎?

編輯:API返回的錯誤是「INVALID_SIGNATURE」。我也嘗試採取相同的值,並通過SoapUI發出請求。使用第一種情況(函數)時會出現同樣的錯誤,並且在使用第二種情況(函數外部)時可以正常工作。那麼,爲什麼在使用函數時會產生錯誤的哈希?

回答

0

我的錯誤。

我沒有將$ key和$ secret傳給genMsg()。

function genNonce() { 
    list($usec, $sec) = explode(" ", microtime()); 
    $nonce = (int)((float)$usec + (float)$sec); 
    return $nonce; 
} 

function genMsg($nonce, $key, $secret) { 
    $signature = strtoupper(base64_encode(hash_hmac('sha256', $nonce . $key, $secret, true))); 
    return $signature; 
} 

$nonce = genNonce(); 
$signature = genMsg($nonce, $key, $secret);